目前JSP有关的比较有名的服务端页面布局/页面组合框架要数sitemesh和tiles了,这是官方文档地址:http://tiles.apache.org/framework/tutorial/index.html
相比较而言tiles的应用更为具体而多样,配置可能稍微复杂,这里记录一下基本的SpringMVC(4.2.5)项目整合tiles3(3.0.7)配置和应用。
应用tiles3需要用到的相关jar包(SpringWEB项目基本包略):
tiles-api-3.0.7.jar tiles-autotag-core-runtime-1.2.jar tiles-core-3.0.7.jar tiles-jsp-3.0.7.jar tiles-request-api-1.0.6.jar tiles-request-jsp-1.0.6.jar tiles-request-servlet-1.0.6.jar tiles-servlet-3.0.7.jar tiles-template-3.0.7.jar commons-beanutils-1.8.0.jar commons-digester-2.0.jar
Spring上下文配置文件中加入启动Bean配置:
<bean id="tilesconfig" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/layout/tiles.xml</value> </list> </property> </bean> <bean id="tilesviewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="order" value="1" /> <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" /> </bean>
建立、配置tiles视图映射文件:
这里用到了tiles配置的继承和重载(重载名为'body'的属性),框架页面为3行布局的形式。
<?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> <definition name="base" template="/WEB-INF/jsp/global/basis.jsp"> <put-attribute name="thetitle" value="FIRST PAGE" type="string"></put-attribute> <put-attribute name="toper" value="/WEB-INF/jsp/global/top.jsp"></put-attribute> <put-attribute name="footer" value="/WEB-INF/jsp/global/bottom.jsp"></put-attribute> <put-attribute name="body" value="/WEB-INF/jsp/index.jsp"></put-attribute> </definition> <!-- 基本扩展 ,继承--> <definition name="homepage" extends="base"> <put-attribute name="body" value="/WEB-INF/jsp/index.jsp"></put-attribute> </definition> <!-- 基本扩展 ,继承--> <definition name="userpage" extends="base"> <put-attribute name="body" value="/WEB-INF/jsp/user/user_list.jsp"></put-attribute> </definition> <!-- 路径通配符匹配,继承 --> <definition name="*/*.page" extends="base"> <put-attribute name="body" value="/WEB-INF/jsp/{1}/{2}.jsp"></put-attribute> </definition> </tiles-definitions>
框架页basis.jsp的body内容部分(需要加入tiles标签库声明):
<table border="0" class="" style="margin:auto;" cellpadding="0" cellspacing="0"> <tr style=""> <td style="line-height: 185px;"> <tiles:insertAttribute name="toper"></tiles:insertAttribute> </td> </tr> <tr> <td style="height: auto;min-height: 490px; height:500px;padding-top: 0;padding-left: 0; text-align: left;" valign="top"> <tiles:insertAttribute name="body" /> </td> </tr> <tr> <td> <tiles:insertAttribute name="footer"></tiles:insertAttribute> </td> </tr> </table>
top页和footer页略。
后台java返回视图的方法:
@RequestMapping(path="/edit",method={RequestMethod.GET}) public String edit(Model mdl,HttpServletRequest request, HttpServletResponse response){ log.info("user/edit"); // return "user/user_edit.page"; }
调试 测试访问:http://localhost:8080/projectname/XXX/edit,能看到内容页面被正确装饰即可。
另外,
也可以不在tiles.xml中静态配置被装饰页面和路径的映射,而直接在需要被装饰的JSP页面的body中嵌套:
加入标签库声明:
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
body中的内容被标签嵌套
<body> <tiles:insertDefinition name="base"> <tiles:putAttribute name="body"> <div> 此种情况需注意body的样式和页面的title ........ </div> </tiles:putAttribute> </tiles:insertDefinition> </body>