Struts2 与 Sitemesh整合示例
一、新建Web工程,例如:s2sitemesh。
二、给工程加入Struts2功能:
引入Struts2 Core Libraries、Struts 2 SiteMesh Libraries库。
三、新建s2sitemesh\WebRoot\index.jsp文件,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SiteMesh Sample Site</title>
</head>
<body>
Welcome to ...
</body>
</html>
四、修改s2sitemesh\WebRoot\WEB-INF\web.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Sitemesh过滤器.start -->
<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>
<!-- Sitemesh过滤器.end -->
<!-- Struts2过滤器.start -->
<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>*.action</url-pattern>
</filter-mapping>
<!-- Struts2过滤器.end -->
</web-app>
五、新建s2sitemesh\WebRoot\WEB-INF\sitemesh.xml文件,内容如下:
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/>
<page-parsers>
<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser"/>
</page-parsers>
<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator"/>
<param name="property.2" value="decorator"/>
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
<param name="match.MSIE" value="ie"/>
<param name="match.Mozilla [" value="ns"/>
<param name="match.Opera" value="opera"/>
<param name="match.Lynx" value="lynx"/>
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
<param name="decorator" value="printable"/>
<param name="parameter.name" value="printable"/>
<param name="parameter.value" value="true"/>
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper">
<param name="decorator" value="robot"/>
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
<param name="decorator.parameter" value="decorator"/>
<param name="parameter.name" value="confirm"/>
<param name="parameter.value" value="true"/>
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
</mapper>
<mapper class="org.apache.struts2.sitemesh.NoneDecoratorMapper">
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}"/>
</mapper>
</decorator-mappers>
</sitemesh>
注:该文件可从sitemesh官网下载的示例中获得。
六、新建s2sitemesh\WebRoot\WEB-INF\decorators.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
<excludes>
<pattern>/index.jsp*</pattern>
<pattern>/login/*</pattern>
</excludes>
<decorator name="main" page="main.jsp"><!-- 定义一个修饰器,name指定 修饰器名,page指定修饰器所使用的JSP页面 -->
<pattern>/*</pattern><!-- 需要修饰的Url范围 -->
</decorator>
</decorators>
七、新建s2sitemesh\WebRoot\decorators\main.jsp文件,内容如下:
<%@ page contentType="text/html;charset=UTF-8"%>
<%@taglib prefix="decorator"
uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>
<html>
<head>
<title><decorator:title /></title>
<decorator:head />
</head>
<body>
<p>
Add head decorator...
</p>
<decorator:body />
<p>
Add foot decorator...
</p>
</body>
</html>
八、新建s2sitemesh\WebRoot\aa.jsp文件,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SiteMesh Sample Site</title>
</head>
<body>
Welcome to ...
</body>
</html>
九、发布测试:OK。
点击:http://localhost:8080/s2sitemesh/index.jsp,输出如下:
Welcome to ...
点击:http://localhost:8080/s2sitemesh/aa.jsp,输出如下:
Add head decorator...
Welcome to ...
Add foot decorator...
十、结束。