sitemesh2作为jsp页装饰器是以java filer形式嵌入jsp项目的,配置好以后,对业务代码基本没有耦合影响,使用方便,中规中矩,一般和Struts2项目结合。
1,webapp的WEB-INF/lib加入必要的库文件,如:
sitemesh-2.4.2.jar
struts2-sitemesh-plugin-2.3.33.jar
2,配置web.xml(与Struts2.3.XX)
<filter> <filter-name>struts-prepare</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class> </filter> <filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> </filter> <filter> <filter-name>struts-execute</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts-prepare</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts-execute</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3,配置decorators.xml(装饰具体的路径)
<?xml version="1.0" encoding="UTF-8"?> <decoratos defaultdir="/jsp"> <decorator name="myDecorator" page="decorator.jsp"> <pattern>/*</pattern> </decorator> <excludes> <pattern>*.html</pattern> <pattern>*.js</pattern> <pattern>*.gif</pattern> <pattern>*.jpg</pattern> <pattern>*.png</pattern> <pattern>*.css</pattern> </excludes> <!-- --> <decorator name="panel" page="/jsp/footer.jsp"></decorator> </decoratos>
4,定义装饰页(模板页)
jsp页面,
引入标签定义:
<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<%@ taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page" %>
在head标签内插入<decorator:head></decorator:head>,以便运行时包含被装饰页的head部分内容,看需要加入<decorator:title></decorator:title>;
在body标签内适当位置插入被装饰页<decorator:body ></decorator:body>,
按需要加入定义的其他页
<page:applyDecorator page="/jsp/footer.jsp" name="panel"></page:applyDecorator>;
sitemesh2的其他标签视需要加入,参考http://wiki.sitemesh.org/wiki/display/sitemesh/Home。