SiteMesh是一个用来在JSP中实现页面布局和装饰的组件框架,使用起来也很简单,说一下配置步骤:
(1)下载sitemesh的组件包,解压,用到的主要有三个文件:解压后目录里的sitemesh的Jar包和META-INF目录下的sitemesh-decorator.tld和sitemesh-page.tld,将两个tld文件放在项目WEB-INF目录下,将Jar包加入到工程中,在工程WebContent目录下创建一个decorators目录(名字随便起),在该目录下新建一个jsp文件,将网页中固定不变的部分都写在这个jsp文件中,比如标题,索引,还有网站的底部。
(2)在WEB-INF下创建decorators.xml,<pattern>指定映射url,<excludes>中指定的url是不会自动嵌入到模版中的,配置如下
<decorators defaultdir="/decorators">
<excludes/>
<decorator name="frame" page="frame.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
Name随便起,路径是这样规定的,defaultdir是模版文件目录,frame.jsp就是刚刚在decorators目录下创建的模版jsp。
(3)在web.xml中配置sitemesh,包括两部分,一个filter,一个jspconfig如下:
<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>
<jsp-config>
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>
/WEB-INF/sitemesh-decorator.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>
/WEB-INF/sitemesh-page.tld</taglib-location>
</taglib>
</jsp-config>
(4)在decorator目录下的frame.jsp中添加
<decorator:title/>
<decorator:body/>
这种标记,它代表的是你将struts2返回的jsp网页中body和title属性自动插入到frame.jsp中。