jar包maven
<!-- Tiles 模板--> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-extras</artifactId> <version>3.0.5</version> </dependency>
springmvc-servlet.xml配置
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"></property> <property name="order" value="1" /> </bean> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> <property name="definitions"> <list> <value>classpath:layout.xml</value> </list> </property> </bean> <!-- 定义JSP文件的位置 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> <property name="order" value="1" /> </bean>
layout.xml
<?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> <!--项目--> <!-- admin --> <!-- admin base --> <definition name="base_admin"> <put-attribute name="admin_head" value="/WEB-INF/pages/common/template/admin/adminHead.jsp"></put-attribute> <put-attribute name="admin_left" value="/WEB-INF/pages/common/template/admin/adminLeft.jsp"></put-attribute> </definition> <!-- 首页 用户管理 --> <definition name="index" template="/WEB-INF/pages/index.jsp" extends="base_admin"> <put-attribute name="flag" type="string" value="1"></put-attribute> </definition> </tiles-definitions>
<definition name="base_admin"> name 对应的controller返回view
jsp页面使用:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%-- --%><%@ include file="/WEB-INF/pages/common/taglibs.jsp"%><%-- --%><%@ include file="/WEB-INF/pages/common/resource.jsp"%><%-- --%><!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <title>首页</title> ${css_main}${css_bootstrap}${js_jquery} <script type="text/javascript" src="/resources/module/admin/index.js"></script> </head> <body> <div class="container"> <div class="row"> <tiles:insertAttribute name="admin_head"></tiles:insertAttribute> <!-- left --> <div class="row"> <tiles:insertAttribute name="admin_left"></tiles:insertAttribute> <div class="col-xs-10"> <input type="hidden" value="<tiles:getAsString name="flag"/>" id="leftv"/> <!--right--> <table class="table table-hover table-striped table-bordered"> <tr> <td>用户名</td> <td>创建时间</td> <td>333</td> </tr> <c:forEach var="u" items="${listUser}"> <tr> <td>${u.userName}</td> <td>${u.createDate}</td> <td>333</td> </tr> </c:forEach> </table> </div> </div> </div> </div> <script type="text/javascript"> </script> </body> </html>
通过<tiles:insertAttribute name="admin_head">就可以引用了
总结:个人感觉tiles标签不是太方便唯一一点就是模板必须是视图返回,然后各种引用,还有各种el表达式的使用,就是返回视图返回模板让我感觉到了不爽,没有sitemesh那么灵活,开始用的tiles用了,现在准备又换到sitemesh试试