Spring MVC下使用tiles框架
html
1. application-context.xml加以下內容:web
<!-- Configures the Tiles layout system --> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles-broadleafdemo.xml</value> </list> </property> </bean>
2. 新建上面寫的tiles-broadleafdemo.xml文件spring
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <definition name="baseNoSide" template="/WEB-INF/jsp/layouts/baseLayoutNoSide.jsp"> <put-attribute name="headContent" value="/WEB-INF/jsp/layouts/headContent.jsp" /> <put-attribute name="navigation" value="/WEB-INF/jsp/snippets/navigation.jsp" /> <put-attribute name="footer" value="/WEB-INF/jsp/layouts/footer.jsp" /> </definition> </tiles-definitions>
3. 創建上面提到的template文件baseLayoutNoSide.jspapache
<%@ include file="/WEB-INF/jsp/include.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Commerce Demo</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <tiles:insertAttribute name="headContent" /> </head> <body> <div> <tiles:insertAttribute name="navigation" /> <tiles:insertAttribute name="mainContent" /> <tiles:insertAttribute name="footer" /> </div> <c:choose> <c:when test="${orderComplete}"> <blc:googleAnalytics webPropertyId="UA-8476611-1" order="${order}" /> </c:when> <c:otherwise> <blc:googleAnalytics webPropertyId="UA-8476611-1" /> </c:otherwise> </c:choose> </body> </html>
4. 分別定義上面的headContent.jsp,navigation.jsp,footer.jsp等等。再此略。。。app
5. JSP調用tiles以下:框架
<%@ include file="/WEB-INF/jsp/include.jsp" %> <tiles:insertDefinition name="baseNoSide"> <tiles:putAttribute name="mainContent" type="string"> <div> <div> <a href="/broadleafdemo/store/equipment/espresso?productId=180"> <img src="/broadleafdemo/images/promos/mainPromo1.gif" /> </a> </div> <div> <div> <a href="/broadleafdemo/store/equipment/cups"> <img src="/broadleafdemo/images/promos/sidePromo1.jpg" /> </a> </div> <div> <a href="/broadleafdemo/store/coffee/starbucks?productId=123"> <img src="/broadleafdemo/images/promos/sidePromo2.jpg" /> </a> </div> </div> </tiles:putAttribute> </tiles:insertDefinition>