struts2与tiles整合(精简版)

一、搭建环境

导入struts2与tiles有关的jar包

1、struts2-blank下的所有jar包(struts2),struts2-tiles-plugin-2.1.8.1.jar(struts2整合tiles的插件)

2、导入commons-digester-2.0.jar,commons-logging-1.0.4.jar,commons-beanutils-1.7.0.jar

3、导入tiles-api-2.0.6.jar,tiles.core-2.0.6.jar,tiles-jsp-2.0.6.jar

 

修改、添加配置文件

1、在web.xml中

    <!-- struts2的中央控制器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Tiles监听器 -->
    <listener>
        <listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
    </listener>

 

2、拷贝struts-2.1.8.1/apps/struts2-showcase(war文件需要解压)/WEB-INF/tiles.xml文件进入本工程src目录下并进行如下修改

     value值可以指定为jsp页面,比如value=“/left.jsp”,我这里只是设置固定string值。

<tiles-definitions>
    <definition name="/welcome" template="/jsp/tiles/layout/index.jsp"> (指定模板页面,其index.jsp页面代码如下,name值前面注意加/)
        <put-attribute name="header" value="Tiles Demo"/>
        <put-attribute name="sidebar" value="this is sidebar"/>
        <put-attribute name="main" value="this is main"/>
        <put-attribute name="footer" value="this is footer"/>
    </definition>
</tiles-definitions>

 

index.jsp的代码

<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
    <head>
        <base href="<%=basePath%>">
    </head>
    <body>
        <table>
            <tr>
                <td colspan="2">
                    header<tiles:insertAttribute name="header"></tiles:insertAttribute>
                </td>
            </tr>
            <tr>
                <td>
                    sidebar<tiles:insertAttribute name="sidebar"></tiles:insertAttribute>
                </td>
                <td>
                    main<tiles:insertAttribute name="main"></tiles:insertAttribute>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    footer<tiles:insertAttribute name="footer"></tiles:insertAttribute>
                </td>
            </tr>
        </table>
        <br>
    </body>
</html>

 

3、在struts.xml文件中做如下配置,并编写相应的action

    <package name="default" namespace="/" extends="struts-default">
       <result-types>
              <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"></result-type>
       </result-types>
       <action name="toIndex" class="action.PageAction" method="toIndex">
               <result name="success" type="tiles">/welcome</result>
       </action>
    </package>

 

public class PageAction extends ActionSupport {
    private static final long serialVersionUID = 7263568517757245698L;
    public String toIndex(){
        return "success";
    }
}

 

访问http://localhost:8080/web工程名/toIndex.action

整合完毕!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值