struts2中使用sitemesh装饰的使用方法和例子

说明:我这里使用struts的版本是2.18版本的,对应的sitemesh是2.42的

必须要的架包有:(除了struts架包以外)

sitemesh-2.42.jar

struts2-sitemesh-plugin-2.2.1.jar

首先我们来配置web.xml

--------------------------------------web.xml中添加如下内容----------------------------------------

<!-- 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>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

------------------------------web.xml结束-------------------------------------------------------------

注意:先配置sitemesh拦截器,在配置struts的拦截器,也就是sitemesh拦截器在struts之前

第二步:

在webroot目录下面建decorators文件夹,里面放装饰的模型jsp(注意:名称必须为:decorators)

我这里名叫mode.jsp,这个名称是可以随便修改的

---------------------------mode.jsp内容--------------------------------------------

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!--引入sitemesh的标签-->
<%@taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jQuery.js"></script>
<decorator:head>

<!--- 其他的页面中head的内容会在这里添加-->

</decorator:head>
</head>
<body>
<div>中华人民共和国万岁 </div>

<!-- 例如:上面的一句话将会在每个页面中添加--->
<decorator:body>

<!--- 其他的页面中body的内容会在这里添加-->

</decorator:body>
</body>
</html>

-------------------------------------------------------------------



最重要的decorators.xml配置 ,其位置和web.xml位置相同

--------------------------------decorators.xml---------------------------------

<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators">
<!--excludes结点则指定了哪些路径的请求不使用任何模板-->
<excludes>
<pattern>/index.jsp*</pattern>
<pattern>/login/*</pattern>
</excludes>
<!--decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板-->
<decorator name="main" page="mode.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值