Spring MVCにtilesフレームを使用する

3665 ワード

Spring MVCではtilesフレームを使用します.
1.appication-context.xmlは以下の内容を追加します.
 
 <!-- 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ファイルを新規作成します.
 
<?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.jspを作成する
 
<%@ 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.上記のheadContect.jsp、navigations.jsp、footer.jspなどをそれぞれ定義します.この略より
 
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>