將tiles模板集成到springMVC框架下,大概流程如下:
1.在配置文件中加入tiles支持
我的servlet配置文件名為spring-mvc.xml.具體配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 激活@controller模式 --> <mvc:annotation-driven /> <!-- 配置包掃描位置 --> <context:component-scan base-package="com.test.maven.controller" /> <!-- 配置視圖解析器(jsp文件前綴后綴) --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <!-- 配置tiles模板 --> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles/tiles-definitions.xml</value> </list> </property> </bean> </beans>
很簡單,只是添加了模板配置文件路徑。
2.配置tiles模板
我們需要給tiles添加一個默認的模板,將一些想要加入的模塊加入進去
<?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="defaultTemplate" template="/WEB-INF/tiles/classic.jsp"> <put-attribute name="title" value="Hello world I'm title" /> <put-attribute name="menu" value="/WEB-INF/tiles/menu.jsp"/> <put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp" /> </definition> </tiles-definitions>
defaultTemplate就是默認模板的名字,template就是模板的路徑
put-attribute表示加入模板的模塊,value指向其路徑
還可以再加入新的模板,如過想要繼承某個模板,可以在definition處去掉template,添加extends(沒試過)
注:在配置模板文件和調用模板時,都應該在文件頭聲明tiles標簽庫
模塊文件就不贅述了,這里說一下模板文件,classic.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title><tiles:getAsString name="title" /></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <div class="page"> <tiles:insertAttribute name="menu" /> <div class="content"> <tiles:insertAttribute name="body" /> </div> <tiles:insertAttribute name="footer" /> </div> </body> </html>
上邊的body元素是模板沒有的,就是所謂的正文內容,因為一般情況下每次調用body內的元素都不一樣,所以無需在模板內指定,在模板文件中指明其位置,然后在調用的時候向里邊填入內容就好了。
3.調用tiles模板
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <body> <tiles:insertDefinition name="defaultTemplate"> <tiles:putAttribute name="body">hahahaha</tiles:putAttribute> <tiles:putAttribute name="footer">hello</tiles:putAttribute> </tiles:insertDefinition> </body>
調用模板,首先是使用defaultTemplate模板,然后配置模板內的組件,若都使用默認,可以直接為空,如果重寫了其中的組件,則會將原模板內的配置覆蓋。但是如果是后加的組件,如上邊的body,並沒有默認的組件,則在調用時必須重寫其內容。
到此,最簡單的tiles配置就結束了。
insertDefinition