好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring boot整合jsp和tiles模板示例

首先贴上我的pox.xml文件,有详细的支持注释说明

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

< strong ><? xml version = "1.0" encoding = "UTF-8" ?>

< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

     xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >

     < modelVersion >4.0.0</ modelVersion >

     < artifactId >demo</ artifactId >

     < version >1.0.1-SNAPSHOT</ version >

     < name >demo</ name >

     < repositories >

         < repository >

             < id >nexus</ id >

             < name >local private nexus</ name >

             < url >http://maven.oschina.net/content/groups/public/</ url >

         </ repository >

         < repository >

             < id >spring-maven-release</ id >

             < name >Spring Maven Release Repository</ name >

             < url >http://maven.springframework.org/release</ url >

         </ repository >

         < repository >

             < id >spring-maven-milestone</ id >

             < name >Spring Maven Milestone Repository</ name >

             < url >http://maven.springframework.org/milestone</ url >

         </ repository >

         < repository >

             < id >mynexus</ id >

             < name >my internal repository</ name >

             < url >http://116.236.223.116:8082/nexus/content/groups/public/</ url >

         </ repository >

     </ repositories >

     < parent >

         < groupId >org.springframework.boot</ groupId >

         < artifactId >spring-boot-starter-parent</ artifactId >

         < version >1.3.0.RELEASE</ version >

         < relativePath /> <!-- lookup parent from repository -->

     </ parent >

     < properties >

         < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >

         < java.version >1.7</ java.version >

     </ properties >

     < dependencies >

         < dependency >

             < groupId >org.springframework.boot</ groupId >

             < artifactId >spring-boot-starter</ artifactId >

             < exclusions >

                 < exclusion >

                     < groupId >org.springframework.boot</ groupId >

                     < artifactId >spring-boot-starter-logging</ artifactId >

                 </ exclusion >

             </ exclusions >

         </ dependency >

         <!--jpa和数据库 -->

         < dependency >

             < groupId >org.springframework.boot</ groupId >

             < artifactId >spring-boot-starter-data-jpa</ artifactId >

             < exclusions >

                 < exclusion >

                     < artifactId >jcl-over-slf4j</ artifactId >

                     < groupId >org.slf4j</ groupId >

                 </ exclusion >

             </ exclusions >

         </ dependency >

         < dependency >

             < groupId >mysql</ groupId >

             < artifactId >mysql-connector-java</ artifactId >

         </ dependency >

         < dependency >

             < groupId >commons-dbcp</ groupId >

             < artifactId >commons-dbcp</ artifactId >

         </ dependency >

         <!--测试 -->

         < dependency >

             < groupId >org.springframework.boot</ groupId >

             < artifactId >spring-boot-starter-test</ artifactId >

             < scope >test</ scope >

             < exclusions >

                 < exclusion >

                     < groupId >org.springframework.boot</ groupId >

                     < artifactId >spring-boot-starter-logging</ artifactId >

                 </ exclusion >

             </ exclusions >

         </ dependency >

         < dependency >

             < groupId >com.github.sgroschupf</ groupId >

             < artifactId >zkclient</ artifactId >

             < version >0.1</ version >

         </ dependency >

         < dependency >

             < groupId >com.xr</ groupId >

             < artifactId >xuri-rpc-api</ artifactId >

             < version >1.0.1-SNAPSHOT</ version >

         </ dependency >

         <!--添加tiles布局模板支持 -->

         < dependency >

             < groupId >org.apache.tiles</ groupId >

             < artifactId >tiles-jsp</ artifactId >

             < version >3.0.5</ version >

         </ dependency >

         <!--添加jstl -->

         < dependency >

             < groupId >javax.servlet</ groupId >

             < artifactId >jstl</ artifactId >

         </ dependency >

         <!--添加对jsp的支持 -->

         < dependency >

             < groupId >org.apache.tomcat.embed</ groupId >

             < artifactId >tomcat-embed-jasper</ artifactId >

             < scope >provided</ scope >

         </ dependency >

         <!--添加访问设备识别支持 -->

         < dependency >

             < groupId >org.springframework.mobile</ groupId >

             < artifactId >spring-mobile-device</ artifactId >

         </ dependency >

         < dependency >

             < groupId >org.json</ groupId >

             < artifactId >json</ artifactId >

         </ dependency >

     </ dependencies >

     < build >

         < plugins >

             <!-- -->

             < plugin >

                 < groupId >org.springframework.boot</ groupId >

                 < artifactId >spring-boot-maven-plugin</ artifactId >

                 < executions >

                     < execution >

                         < goals >

                             < goal >repackage</ goal >

                         </ goals >

                     </ execution >

                 </ executions >

             </ plugin >

             <!--maven install时 跳过单元测试 -->

             < plugin >

                 < groupId >org.apache.maven.plugins</ groupId >

                 < artifactId >maven-surefire-plugin</ artifactId >

                 < configuration >

                     < skip >true</ skip >

                 </ configuration >

             </ plugin >

         </ plugins >

     </ build >

     < packaging >war</ packaging >

</ project >

</ strong >

在application.properties文件中需要加入配置如下

?

1

< strong >spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp</ strong >

1.接下来我们写一个titles支持的模板jsp

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

< strong ><%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

< html >

     < head >

         < title >< tiles:getAsString name = "title" /></ title >

     </ head >

     < body >

         <!-- Header 可以被put-attribute name="header] value 所描述的文件替换-->

         < tiles:insertAttribute name = "header" />

         <!-- Body -->

         < tiles:insertAttribute name = "body" />

         <!-- Footer -->

         < tiles:insertAttribute name = "footer" />

     </ body >

</ html >

</ strong >

2.现在可以写一个整合titles的配置文件了tiles.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<? xml version = "1.0" encoding = "utf-8" ?>

<!DOCTYPE tiles-definitions PUBLIC

        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"

        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

< tiles-definitions >

     <!-- Templates -->

     < definition name = "layout.basic" template = "/WEB-INF/jsp/layout/basic.jsp" >

         < put-attribute name = "title" value = "Spring Web MVC with Tiles 3" />

         < put-attribute name = "header" value = "/WEB-INF/jsp/view/header.jsp" />

         < put-attribute name = "body" value = "" />

         < put-attribute name = "footer" value = "/WEB-INF/jsp/view/footer.jsp" />

     </ definition >

             <!-- Pages -->    

     < definition name = "site.greeting" extends = "layout.basic" >

         < put-attribute name = "body" value = "/WEB-INF/tiles/view/home/greeting.jsp" />

     </ definition >

</ tiles-definitions >

 3.最后我们需要加载titles.xml文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

@Configuration

public class ConfigurationForTiles {

     @Bean

     public TilesConfigurer tilesConfigurer() {

         final TilesConfigurer configurer = new TilesConfigurer();

         configurer.setDefinitions( new String[] { "WEB-INF/jsp/tiles.xml" });

         configurer.setCheckRefresh( true );

         return configurer;

     }

     @Bean

     public TilesViewResolver tilesViewResolver() {

         final TilesViewResolver resolver = new TilesViewResolver();

         resolver.setViewClass(TilesView. class );

         return resolver;

     }

}

以上就是Spring boot整合jsp和tiles模板示例的详细内容,更多关于Spring boot整合jsp和tiles模板的资料请关注其它相关文章!

原文链接:http://www.kailing.pub/article/index/arcid/60.html

查看更多关于Spring boot整合jsp和tiles模板示例的详细内容...

  阅读:13次