struts2学笔记九 整合SiteMesh

本文介绍如何使用SiteMesh框架进行页面装饰,并详细说明了其与Struts2框架的整合步骤。从配置web.xml到定义装饰器页面,再到整合Struts2,提供了完整的实施流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sitemesh一种装饰框架
1.web.xml
<!-- 定义整合SiteMesh和核心 Filter -->
 <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>

2 jsp页面
导入sitemesh标签库
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>

3 sitemesh-2.3.jar
4第一个装饰页
<page:applyDecorator page="/WEB-INF/decorators/book.html" name="panel" />

 <decorator:title/>
 <decorator:body />

与struts2整合
1 struts2-sitemesh-plugin-2.0.6.jar

2 配置web.xml
注意顺序
ActionContextCleanUp过滤器
SiteMesh核心过滤器
FilterDispatcher过滤器
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4">

 <!-- 用于初始化Spring容器的Listener -->
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

 <!-- 定义整合SiteMesh必须的ActionContextCleanUp Filter -->
 <filter>
  <filter-name>struts-cleanup</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.ActionContextCleanUp
  </filter-class>
 </filter>
 <!-- 定义整合SiteMesh和核心 Filter -->
 <filter>
  <filter-name>sitemesh</filter-name>
  <filter-class>
   com.opensymphony.module.sitemesh.filter.PageFilter
  </filter-class>
 </filter>
 <!-- 定义Struts2的FilterDispathcer的Filter -->
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </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>
 <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>
3定义装饰页
4decorators.xml文件来配置装饰器页面
<?xml version="1.0" encoding="GBK"?>

<decorators defaultdir="/WEB-INF/decorators">
    <!-- 在excludes元素下指定的页面将不会由SiteMesh来装饰 -->
    <excludes>
    </excludes>

 <!-- 创建一个名为main的装饰器,该装饰器页面为main.jsp,
      用于装饰pattern指定的URL的所有页面-->
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>

 <!-- 定义一个装饰器,但该装饰器默认不装饰任何页面 -->
    <decorator name="panel" page="panel.jsp"/>
</decorators>

在WebRoot目录下的jsp页面自动被装饰,
?在文件夹里面的怎么办???

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值