因为现在网上的没有找到相关的文档,自己整理了一个。
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>
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"/>
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/下。