笼统理解:
通过filter包装request和response,实现模板功能。
具体实现:
首先,将sitemesh的jar复制到web项目的lib目录下
然后,编写sitemesh的三个配置文件
第一、编写web.xml
目的:
1、将sitemesh框架集成到本web应用中去。
具体:
<filter
>
<filter
-name
>sitemeshFilter
<
/filter
-name
>
<filter
-
class
>org.springframework.web.filter.DelegatingFilterProxy
<
/filter
-
class
>
<init
-param
>
<param
-name
>targetBeanName
<
/param
-name
>
<param
-value
>sitemeshFilterBean
<
/param
-value
>
<
/init
-param
>
<init
-param
>
<param
-name
>targetFilterLifecycle
<
/param
-name
>
<param
-value
>
true
<
/param
-value
>
<
/init
-param
>
<
/filter
>
<filter
-mapping
>
<filter
-name
>sitemeshFilter
<
/filter
-name
>
<url
-pattern
>
/mobile
/*</url-pattern>
</filter-mapping>
2、更改sitemesh.xml的路径,否则会使用sitemesh.xml的默认位置:/WEB-INF/sitemesh.xml
<context
-param
>
<!--the variable is setted by the DefaultFactory
-->
<param
-name
>sitemesh.configfile
<
/param
-name
>
<param
-value
>
/WEB
-INF
/classes
/conf
/sitemesh.xml
<
/param
-value
>
<
/context
-param
>
第二,编写sitemesh.xml:设置sitemesh框架的一些主要信息,例如使用什么解释器来解释page,以及decorators.xml位置等
1、文件位置可以参考上面
2、使用官方的默认设置,如下面所示:
<sitemesh
>
<property name
=
"decorators-file" value
=
"/WEB-INF/classes/conf/decorators.xml"
/
>
<excludes file
=
"${decorators-file}"
/
>
<!--使用以下的parser可以有效解决中文乱码问题-->
<page
-parsers
>
<parser
default
=
"true"
class
=
"com.opensymphony.module.sitemesh.parser.HTMLPageParser"
/
>
<parser content
-type
=
"text/html"
class
=
"com.opensymphony.module.sitemesh.parser.HTMLPageParser"
/
>
<parser content
-type
=
"text/html;charset=UTF-8"
class
=
"com.opensymphony.module.sitemesh.parser.HTMLPageParser"
/
>
<
/page
-parsers
>
<decorator
-mappers
>
<mapper
class
=
"com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper"
>
<param name
=
"decorator" value
=
"blank"
/
>
<
/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.ConfigDecoratorMapper"
>
<param name
=
"config" value
=
"${decorators-file}"
/
>
<
/mapper
>
<
/decorator
-mappers
>
<
/sitemesh
>
第三、编写decorators.xml,设置什么页面需要怎么样的decorator进行包装
<?xml version
=
"1.0" encoding
=
"UTF-8"?
>
<!-- 设置用于包装的模板所在的位置
-->
<decorators defaultdir
=
"/decorators"
>
<!-- decorator实例
-->
<!--decMobile.jsp为 名为“mobile”的decorator 所使用的模板页面
-->
<decorator name
=
"mobile" page
=
"decMobile.jsp"
>
<pattern
>
/mobile
/*</pattern>
</decorator>
<excludes>
<pattern>/403*</pattern>
<pattern>/404*</pattern>
<pattern>/500*</pattern>
<pattern>/login*</pattern>
</excludes>
</decorators>
最后,编写包装使用到的模板页面
如:
<%@ page language
=
"java" contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<!-- 使用了sitemesh的标签,所以要进行有关的一些设置
-->
<%@ taglib uri
=
"http://www.opensymphony.com/sitemesh/decorator" prefix
=
"decorator"
%>
<%@ taglib prefix
=
"c" uri
=
"http://java.sun.com/jsp/jstl/core"
%>
<c
:set var
=
"ctxPath" value
=
"${pageContext.request.contextPath}"
/
>
<?xml version
=
"1.0" encoding
=
"UTF-8"?
>
<!DOCTYPE html PUBLIC
"-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
"http://www.wapforum.org/DTD/xhtml-mobile10.dtd"
>
<html xmlns
=
"http://www.w3.org/1999/xhtml"
>
<head
>
<link rel
=
"stylesheet" type
=
"text/css" href
=
"<%=request.getContextPath()%>/mobile/style.css"
/
>
<meta http
-equiv
=
"Content-Type" content
=
"application/xhtml+xml; charset=UTF-8"
/
>
<title
>
<!--
"decorator:xxx"代表被包装的页面的对应部分
-->
<decorator
:title
default
=
"欢迎使用移动办公管理系统"
/
>
<
/title
>
<decorator
:head
/
>
<
/head
>
<body
>
<decorator
:body
/
> 
;
<
/body
>
<
/html
>
具体参考这篇好文章:
http://docs.huihoo.com/java/sitemesh/index.html
api:
http://www.opensymphony.com/sitemesh/api/overview-summary.html
项目相关内容:
https://github.com/sitemesh/sitemesh2