天天看点

struts2之整合sitemesh(sitemesh配置)

struts2之整合sitemesh(sitemesh配置)

----------

1.下载sitemesh-xxx.jar文件并复制到web应用的WEB0-INF/lib路径下。因为与struts2整合,所以我们还需要struts2-sitemesh-plugin-xxx.jar文件,如果使用maven,我们可以直接使用,如例:

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-sitemesh-plugin</artifactId>

<version>2.0.11.2</version>

</dependency> 

struts2-sitemesh-plugin包会将依赖的sitemesh包也一并包含到项目中。

2.为了使sitemesh框架能够处理所有的用户请求,还必须在web.xml文件中配置sitemesh框架的核心Filter。配置sitemesh的核心Filter的配置片段如下:

<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> 

3.为了在jsp页面中使用sitemesh的标签库,则还需要导入sitemesh标签库。如果使用Servlet2.4以上规范,则无需任何修改,直接在jsp页面中使用taglib指令导入标签库即可。

sitemesh提供了两个标签库,因此可以在jsp页面中使用如下两行来导入标签库:

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>

<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%> 

当然,这些标签库通常只在sitemesh的装饰器页面中使用,无需在原始的jsp页面中使用。

Servlet2.4规范会自动加载WEB-INF/lib路径下的所有jar文件,并自动注册每个jar文件的META-INF路径下的标签库定义文件。

如果使用Servlet2.3以前的版本,则web应用不会自动加载WEB-INF/lib路径下jar文件中包含的标签库定义文件,应该在web.xml文件中使用<taglib.../>元素来增加标签库定义,增加标签库定义,需要增加如下代码片段:

<taglib>

<taglib-uri>http://www.opensymphony.com/sitemesh/decorator</taglib-uri>

<taglib-location>/WEB-INF/lib/sitemesh-2.3.jar</taglib-location>

</taglib>

<taglib>

<taglib-uri>http://www.opensymphony.com/sitemesh/page</taglib-uri>

<taglib-location>/WEB-INF/lib/sitemesh-2.3.jar</taglib-location>

</taglib> 

继续阅读