`

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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics