springmvc 整合apache tiles

   最近把springmvc3.2.0 和 apache tiles 整合了一下,并用mvn构建了web工程,下面把关键点说明一下。前提:springmvc3.2 已经搭建完毕,搭建方式可查看
http://zld406504302.iteye.com/blog/1887537
   1:pom.xml 依赖项
     
     <!--tiles jar 依赖-->
     <dependency>
		<groupId>org.apache.tiles</groupId>
		<artifactId>tiles-extras</artifactId>
		<version>3.0.1</version>
     </dependency>
     <!--jstl 依赖 (构建tiles模板会用到)-->
     <dependency>
     	<groupId>javax.servlet</groupId>
     	<artifactId>jstl</artifactId>
     	<version>1.2</version>
     </dependency>
     

 
   2:创建tiles.xml 文件,并构建tiles模板
       2.1 目录结构
          [img]
springmvc 整合apache tiles
[/img]
      2.2 文件内容
        tiles.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>
    <definition name="baselayout" template="/WEB-INF/tiles/baselayout.jsp" preparer="com.cn.ld.framework.web.tiles.CustomerPreparer">
        <put-attribute name="title" value="title" />
        <put-attribute name="login_user" value="" />
        <put-list-attribute name="stylesheets">
            <add-attribute value="static/style/main" />
        </put-list-attribute>
        <put-list-attribute name="scripts">
            <add-attribute value="static/js/util" />
        </put-list-attribute>
    </definition>
    
    <definition name="sublayout" extends="baselayout">
        <put-attribute name="login_user" value="/WEB-INF/tiles/loginUser.jsp" />
    </definition>
    
    <definition name="title" template="/WEB-INF/tiles/title.jsp">
        <put-attribute name="title" value="springmvc3.2" />
    </definition>
    
    <definition name="user/login/home" extends="baselayout">
        <put-attribute name="title" value="home page" />
        <put-attribute name="main" value="/WEB-INF/jsp/user/login/home.jsp" />
    </definition>
    
    <definition name="home" extends="baselayout">
        <put-attribute name="main" value="/WEB-INF/tiles/loginUser.jsp" />
        <put-list-attribute name="scripts" inherit="true">
        </put-list-attribute>
    </definition>
 
</tiles-definitions>

       

      
      baselayout.jsp
      
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles-extras" prefix="tilesx" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
        + request.getServerName() + ":" + request.getServerPort()
        + path + "/";
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <!-- tile <put-attribute name="title" value="title" />-->
        <title><tiles:insertAttribute name="title" /></title>
        <!-- stylesheets 要和tiles <put-list-attribute name="stylesheets"> name 一致 -->
        <!-- classname 定义类型 -->
        <tilesx:useAttribute name="stylesheets" classname="java.util.List" />
        <c:forEach items="${stylesheets}" var="src">
          <link rel="stylesheet" type="text/css" href="<%=basePath %><c:url value='${src}.css' />" />
        </c:forEach>
        <tilesx:useAttribute id="scripts" name="scripts" classname="java.util.List" />
        <c:forEach items="${scripts}" var="src">
            <script type="text/javascript" src="<%=basePath %><c:url value='${src}.js' />"></script>
        </c:forEach>
    </head>
    <body>
        <div id="head">
        <div id="banner"></div>
        <div id="menu"><jsp:include page="/WEB-INF/tiles/menu.jsp"/></div>	
        </div>
        <!-- 加载 tiles main 指向的页面<put-attribute name="main" value="/WEB-INF/jsp/user/login/home.jsp" /> -->
        <div id="main"><tiles:insertAttribute name="main" /></div>
        <div id="footer"><jsp:include page="/WEB-INF/tiles/footer.jsp"/></div>
    </body>
</html>
       


       footer.jsp
      
       	Copyright 2013 springmvc3.2, developer java 小生

       


       loginUser.jsp
      
        <h1>Hello, ${user.name}  ${user.age }  ${user.sex}</h1>
       

      
         menu.jsp
      
        <div>
            [list]
               [*]1

               [*]2

               [*]3

               [*]4

            [/list]
        </div>
       


       sublayout.jsp
      
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles-extras" prefix="tilesx" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <tiles:insertAttribute name="login_user" />
       


       title.jsp
      
       <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<title>springmvc3.2</title>
       



   3:dispatcher-servlet.xml 增加tiles view试图
    
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" p:order="1"/>
    
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>
    
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/"
			p:suffix=".jsp" p:order="2"/>

    

    InternalResourceViewResolver 和 TilesViewResolver 共存,且以TilesViewResolver 为首要匹配,通过 p:order 来控制级别。数字越大,级别越低。

     启动tomcat ,感受效果吧。

你可能感兴趣的:(apache,springMVC,tiles,tiles,tiles,dispatcher,and,整合apache)