难道没有人用过tiles框架么??

解决方案 »

  1.   

    最近闲着无事,琢磨了一下struts2.06的tiles使用。以下是一些简单使用的步骤。
    1.在WEB-INF/lib下加入所需的jar包 
        commons-digester-1.6.jar,
        xwork-2.0.1.jar,
        tiles-core-2.0-20070207.130156-4.jar,
        tiles-api-2.0-20070207.130156-4.jar,
        struts2-tiles-plugin-2.0.6.jar,
        struts2-core-2.0.6.jar
    2.修改web.xml  为以下内容
       <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
         <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
         <listener>
            <listener-class>org.apache.tiles.listener.TilesListener</listener-class>
        </listener>
    </web-app>3.在WEB-INF下添加tiles.xml文件
    <?xml version="1.0" encoding="UTF-8" ?>
       <!DOCTYPE tiles-definitions PUBLIC
            "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
            "http://struts.apache.org/dtds/tiles-config_2_0.dtd">
    <tiles-definitions>
        <definition name="showcase.index" template="/tiles/layout.jsp">
            <put name="title"  value="Tiles Showcase"/>
            <put name="header"  value="/tiles/header.jsp"/>
            <put name="body"  value="/tiles/body.jsp"/>
        </definition>
    </tiles-definitions>
    4.struts.xml 为:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <constant name="struts.enable.DynamicMethodInvocation" value="true" />
        <constant name="struts.devMode" value="true" />
        <constant name="struts.ui.theme" value="simple" />
        <include file="struts-conf/struts-test.xml" /> 
    </struts>
    5.struts-test.xml文件为:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <package name="test" namespace="/test" extends="tiles-default">
        <!-- <result-types>
                  <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
            </result-types>  -->
            <global-results>
                <result name="login"><param name="location">/user/login.jsp</param></result>
            </global-results>  
        <action name="sample" class="com.lhx.test.action.TilesAction" method="inputIndex" >
              <result name="success" type="tiles">showcase.index</result> 
        </action>
        </package>
    </struts>
    6.注意要在每个jsp文件中加入<%@ page contentType="text/html; charset=UTF-8"%>