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試試