struts2 使用装饰页面 /decorators

此为一个非常简单的例子

在web工程的web.xml 的文件进行配置

   <!--用来消除sitemesh 拦截器 Struts2拦截器的冲突 使之兼容-->      
<filter>  
    <filter-name>struts-cleanup</filter-name>  
    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>  
</filter>  
  
<filter>  
    <filter-name>sitemesh</filter-name>  
    <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>  
</filter>  
      
<filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
</filter>  
  
<filter-mapping>  
    <filter-name>struts-cleanup</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>  
  
<filter-mapping>  
    <filter-name>sitemesh</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>  
  
<filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>  

此配置默认的index.jsp 页面不会使用装饰页面,若将红色的去掉将会使index.jsp页面使用装饰页面

配置完web.xml页面之后,在web.xml的同层目录下建立一个decorators.xml文件,用于配置

<?xml version="1.0" encoding="utf-8"?>  

<decorators defaultdir="/decorators">   //装饰的页面所在的文件目录

 
     
    <decorator name="main" page="main.jsp">  
        <pattern>/*</pattern>    //对何种action响应进行装饰 ,此为对所有的action进行装饰。可以对特定的页面进行特定的装饰
    </decorator>  
    
</decorators>  

然后在WebContent目录下的decorators新建main.jsp装饰页面

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html lang="zh-cn">
    <head>  
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
        
    
        <title><decorator:title default="U 社区"/></title>
          
        <%-- 从被装饰页面获取head标签内容 --%>  
        <decorator:head/>  

 
        
    </head>  
  
    <body>  
        <%-- 从被装饰页面获取body标签内容 --%>  
        <decorator:body />
        
        this is decorator page.
    </body>  
</html>  

装饰页面可以将一些css,js等资源引入,或者一些公共的属性操作。

此工程需要导入相关的struts2的开发包

源码地址:http://pan.baidu.com/s/1i39Ws2T
原文地址:https://www.cnblogs.com/songyao/p/4171717.html