在struts2中加入装饰器sitemesh

来源:互联网 发布:软件破解器 编辑:程序博客网 时间:2024/05/22 14:21

装饰器可以统一设置页面的版式,统一引入需要使用的JS,CSS等等作用。

1. 导入sitemesh-xxx.jar包

2. 在web.xml中进行配置

 <!-- 定义ActionContextCleanUp过滤器 -->
 <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>

 

  <!--

   注意ActionContextCleanUp过滤器必须在FilterDispatcher之前配置,

  ActionContextCleanUp的主要功能是通知FilterDispatcher执行完毕不要清除

  ActionContext,以便sitemesh装饰器可以访问Struts值堆栈。

  -->

 

 <!-- 定义SiteMesh的核心过滤器 -->
 <filter>
  <filter-name>sitemesh</filter-name>
  <filter-class>
   com.opensymphony.module.sitemesh.filter.PageFilter
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>sitemesh</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 

 <!-- 使用STRUTS2 -->
 <filter>
  <filter-name>struts</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 

 注意三个过滤器的前后顺序,不能换!~

3. 在WEB-INF目录下配置decorators.xml。如下demo

<decorators defaultdir="/decorators">
 <excludes>

     <pattern>*.html</pattern>
     <pattern>/pages/login.do</pattern>
  </excludes>
   
 <decorator name="main" page="main.jsp">
  <pattern>/*</pattern>
 </decorator>
 
 <decorator name="header" page="header.jsp" />
 <decorator name="fooder" page="footer.jsp" />
</decorators>

4. 在decorators目录下,编写JSP页面

 <%--  footer.jsp  --%>

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
 <div>
  退出 帮助<br/>
  <s:property value="footerTime"/><br/>
  版权所有&copy; 2009
 </div>

 

   <%--  main.jsp --%>

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title><decorator:title default="同学" />_湖南</title>
   <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
   <link href="<%= request.getContextPath() %>/css/style.css" rel="stylesheet" type="text/css" />
  <decorator:head />
 </head>
 <body <decorator:getProperty property="body.class" writeEntireProperty="true" />>
  <page:applyDecorator name="header" />
  <div class="a b">
         <decorator:body />
    </div>
  <page:applyDecorator name="fooder" />

    </body>

</html>

原创粉丝点击