struts2.3和sitemesh3整合

因为现在网上的没有找到相关的文档,自己整理了一个。

1、创建MyEclipse项目

因为工具最多支持struts2.1 都知道这个版本有漏洞。我们自己使用2.3

*添加需要的类库

*修改web.xml添加监控器

*在src下添加struts.xml,并添加action配置

*创建继承actionsupport.java的action类

具体步骤可以看前一个博客

2、添加sitemesh3需要的jar包

3、在web.xml中添加过滤器,完整的web.xml为

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	

<filter>  
    <filter-name>sitemesh3</filter-name>  
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</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>sitemesh3</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>  
  
<filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>  
  
  
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
</web-app>

这里有一个顺序和命名的问题,sitemesh放在struts2上层拦截,sitemesh的filter-name 不能命名为sitemesh 。

4、在WEB-INF下添加sitemesh3.xml

<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
  <mapping path="/songs1/*" decorator="/decorators/decorator.jsp"/>
  <mapping path="/songs/*" decorator="/myDecorator.jsp"/>

<!-- 配置默认的渲染器. 将应用于所有路径. -->
  <mapping decorator="/default-decorator.html"/>

  <!-- 配置特定路径的渲染器. -->
  <mapping path="/admin/*" decorator="/another-decorator.html"/>
  <mapping path="/*.special.jsp" decorator="/special-decorator.html"/>

  <!-- 配置多个渲染器. -->
  <mapping>
    <path>/articles/*</path>
    <decorator>/decorators/article.html</decorator>
    <decorator>/decorators/two-page-layout.html</decorator>
    <decorator>/decorators/common.html</decorator>
  </mapping>

  <!-- 不被渲染的路径. -->
  <mapping path="/javadoc/*" exclue="true"/>
  <mapping path="/brochures/*" exclue="true"/>

</sitemesh>

5、webRoot下创建myDecorator.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>装饰器页面<sitemesh:write property='title'/></title>
<sitemesh:write property='head' />
</head>
<body>
<div>top</div>
<hr>
<div>
<sitemesh:write property='body'/>
</div>
<hr>
<div>bottom</div>
</body>
</html>

6、被装饰的页面不需要做任何改动。

结果为:


总结:struts2.3和sitemesh3都是采用过滤器的实现方式;过滤器在web.xml中实现。

           struts2需要一个配置文件,名称固定为struts.xml放在src下,当然也可以直接放在编译后的classes下。

          sitemesh3的配置文件名sitemesh3.xml放在webRoot/WEB-INF/下。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值