天天看點

在struts2中整合sitemesh

今天在做個整合時,決定用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裝配就完成了