欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在struts2中整合sitemesh

程序员文章站 2022-03-05 12:42:23
...

今天在做个整合时,决定用sitemesh把几个页面的布局整理下。

 

很久没使用过了,咋一用还出了不少问题,幸亏网上资料比较多。所以还算比较顺利的解决了。

 

总结一下

 

我的版本是struts2 2.1.8

 

1、struts2 中使用sitemesh一定要用struts2 的插件 struts2-sitemesh-plugin-****.jar

否则是没有效果的

 

2、在web.xml中的filter顺序

 

    <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     <filter>
        <filter-name>sitemesh</filter-name>
    	<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>
    <filter-mapping>
    	<filter-name>sitemesh</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>  
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>struts-default.xml,struts-plugin.xml,struts.xml,struts-tica.xml</param-value>
        </init-param>
    </filter>

 

sitemesh的filter一定是在

ActionContextCleanUp 和 StrutsPrepareAndExecuteFilter

之间的,千万别放错了,否则一定出问题

 

3、上述的弄完之后,可以建立你的模版文件了,下面,我都用自己的页面来举例了

/decorators/basic-theme.jsp

 

<%@page contentType="text/html; charset=UTF-8"%>
<%@ page pageEncoding="UTF-8" %>
<%@page import="com.qeweb.framework.common.Envir"%>
<%@page import="com.qeweb.tica.vendormgt.action.RegistAC"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ include file="/WEB-INF/tica/common/jsp_includes.jsp"%>
<%
    response.setHeader("Cache-Control", "no-cache"); //HTTP   1.1
    response.setHeader("Pragma", "no-cache");        //HTTP 1.0
    response.setDateHeader("Expires", 0);
%>
<% String wwwroot = request.getContextPath();%>

<html>
<head>
<decorator:head></decorator:head>
</head>
<body>
   <body style="width: 100%;margin: 0 auto;" >
    <center>
        <div>
            <DIV class="topArc" style="height: 80px;"><img src="<%=ctx %>/framework/images/preference/theme/logo/<%=DisplayType.getThemeType() %>.jpg" width="100%" height="100%"></DIV>
            <div id="rbody" class="wrap">
                <div class="title">
                    <h1></h1>
                </div>
    				<decorator:body></decorator:body>
            </div>
        </div>
    </center>
	<center>
		<font size="2" color="#555555"></font>
	</center>
</body>
</html>

 

4、装饰配置文件

/WEB-INF/decorators.xml

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
	<excludes>       
        <pattern>*.js</pattern>
        <pattern>*.gif</pattern>
        <pattern>*.jpg</pattern>
        <pattern>*.png</pattern>
        <pattern>*.css</pattern>
    </excludes>
	

    <decorator name="basic-theme" page="basic-theme.jsp">
        <pattern>/ticavendor/regist*.action</pattern>
    </decorator>
</decorators>

 

至此,最简单的一个sitemesh装配就完成了