SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-005- 使用ApacheTiles(TilesConfigurerTilesViewResolver&(代

Posted shamgod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-005- 使用ApacheTiles(TilesConfigurerTilesViewResolver&(代相关的知识,希望对你有一定的参考价值。

一、

1.定义TilesConfigurer、TilesViewResolver的bean

注意有tiles2和tiles3,这里使用tiles3

(1)java形式

 1 package spittr.web;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.ComponentScan;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.servlet.ViewResolver;
 7 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
 8 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 9 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
10 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
11 import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
12 import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
13 
14 @Configuration
15 @EnableWebMvc
16 @ComponentScan("spittr.web")
17 public class WebConfig extends WebMvcConfigurerAdapter {
18 
19   @Override
20   public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
21     configurer.enable();
22   }
23   
24   @Override
25   public void addResourceHandlers(ResourceHandlerRegistry registry) {
26     // TODO Auto-generated method stub
27     super.addResourceHandlers(registry);
28   }
29 
30   
31   // Tiles
32   @Bean
33   public TilesConfigurer tilesConfigurer() {
34     TilesConfigurer tiles = new TilesConfigurer();
35     tiles.setDefinitions(new String[] {
36         "/WEB-INF/layout/tiles.xml",
37         "/WEB-INF/views/**/tiles.xml"
38     });
39     tiles.setCheckRefresh(true);
40     return tiles;
41   }
42   
43   @Bean
44   public ViewResolver viewResolver() {
45     return new TilesViewResolver();
46   }
47   
48 }

 

(2)xml形式

1 <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
2     <property name="definitions">
3         <list>
4             <value>/WEB-INF/layout/tiles.xml.xml</value>
5             <value>/WEB-INF/views/**/tiles.xml</value>
6         </list>
7     </property>
8 </bean>
9 <bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />

 

2.定义layout文件tiles.xml

 1 <?xml version="1.0" encoding="ISO-8859-1" ?>
 2 <!DOCTYPE tiles-definitions PUBLIC
 3        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
 4        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
 5 <tiles-definitions>
 6 
 7   <definition name="base" template="/WEB-INF/layout/page.jsp">
 8     <put-attribute name="header" value="/WEB-INF/layout/header.jsp" />
 9     <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
10   </definition>
11 
12   <definition name="home" extends="base">
13     <put-attribute name="body" value="/WEB-INF/views/home.jsp" />
14   </definition>
15 
16   <definition name="registerForm" extends="base">
17     <put-attribute name="body" value="/WEB-INF/views/registerForm.jsp" />
18   </definition>
19 
20   <definition name="profile" extends="base">
21     <put-attribute name="body" value="/WEB-INF/views/profile.jsp" />
22   </definition>
23 
24   <definition name="spittles" extends="base">
25     <put-attribute name="body" value="/WEB-INF/views/spittles.jsp" />
26   </definition>
27 
28   <definition name="spittle" extends="base">
29     <put-attribute name="body" value="/WEB-INF/views/spittle.jsp" />
30   </definition>
31 
32 </tiles-definitions>

 

home相当于

1 <definition name="home" template="/WEB-INF/layout/page.jsp">
2     <put-attribute name="header" value="/WEB-INF/layout/header.jsp" />
3     <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
4     <put-attribute name="body" value="/WEB-INF/views/home.jsp" />
5 </definition>

 

3.定义模板文件page.jsp

 1 <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
 2 <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t" %>
 3 <%@ page session="false" %>
 4 <html>
 5   <head>
 6     <title>Spittr</title>
 7     <link rel="stylesheet" 
 8           type="text/css" 
 9           href="<s:url value="/resources/style.css" />" >
10   </head>
11   <body>
12     <div id="header">
13       <t:insertAttribute name="header" />
14     </div>
15     <div id="content">
16       <t:insertAttribute name="body" />
17     </div>
18     <div id="footer">
19       <t:insertAttribute name="footer" />
20     </div>
21   </body>
22 </html>

 

4.其他jsp文件

(1)header.jsp

1 <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
2 <a href="<s:url value="/" />"><img 
3     src="<s:url value="/resources" />/images/spitter_logo_50.png" 
4     border="0"/></a>

(2)footer.jsp

1 Copyright &copy; Craig Walls

(3)home.jsp

1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
2 <%@ page session="false" %>
3 <h1>Welcome to Spitter</h1>
4 
5 <a href="<c:url value="/spittles" />">Spittles</a> | 
6 <a href="<c:url value="/spitter/register" />">Register</a>

(4)registerForm.jsp

 1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 2 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>
 3 <%@ page session="false" %>
 4 <h1>Register</h1>
 5 
 6 <sf:form method="POST" commandName="spitter" >
 7   <sf:errors path="*" element="div" cssClass="errors" />
 8   <sf:label path="firstName" 
 9       cssErrorClass="error">First Name</sf:label>: 
10     <sf:input path="firstName" cssErrorClass="error" /><br/>
11   <sf:label path="lastName" 
12       cssErrorClass="error">Last Name</sf:label>: 
13     <sf:input path="lastName" cssErrorClass="error" /><br/>
14   <sf:label path="email" 
15       cssErrorClass="error">Email</sf:label>: 
16     <sf:input path="email" cssErrorClass="error" /><br/>
17   <sf:label path="username" 
18       cssErrorClass="error">Username</sf:label>: 
19     <sf:input path="username" cssErrorClass="error" /><br/>
20   <sf:label path="password" 
21       cssErrorClass="error">Password</sf:label>: 
22     <sf:password path="password" cssErrorClass="error" /><br/>
23   <input type="submit" value="Register" />
24 </sf:form>

 

5.css等文件

 1 body {
 2     background: #ff9933 url(images/spitter_background.gif) no-repeat
 3         fixed left top;
 4     color: #333333;
 5     font-family: sans-serif;
 6     font-size: 0.75em;
 7     font-size-adjust: none;
 8     font-stretch: normal;
 9     font-style: normal;
10     font-variant: normal;
11     font-weight: normal;
12     line-height: 1.5;
13     text-align: center;
14 }
15 
16 .spittleList h1 {
17     font-size: 16pt;
18 }
19 
20 .spittleList ul {
21     list-style: none;
22     margin-left: 0px;
23     padding-left: 0px;
24 } 
25 
26 .spittleList ul li:first-child {
27     border-top: 0px;
28 } 
29 
30 .spittleList ul li {
31     border-top: 1px solid #cccccc;
32 } 
33 
34 .spittleTime {
35     font-size: 8pt;
36 }
37 
38 .spittleLocation {
39     font-size: 8pt;
40 }
41 
42 .spittleForm h1 {
43     font-size: 16pt;
44 }

 

6.运行结果:

(1)http://localhost:8080/SpringInAction4_Chapter6_ApacheTiles/home

技术分享

(2)http://localhost:8080/SpringInAction4_Chapter6_ApacheTiles/register

技术分享

 

以上是关于SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-005- 使用ApacheTiles(TilesConfigurerTilesViewResolver&(代的主要内容,如果未能解决你的问题,请参考以下文章

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-005- 使用ApacheTiles(TilesConfigurerTilesViewResolver&(代

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY简介及用<s:message>和R

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@ScopeProxyMode

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect

SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMappingmodel