`
wtnhwbb
  • 浏览: 164186 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struts 2.0 整合Sitemesh

阅读更多

概述

        Struts 2.0提供一个Sitemesh插件,允许在Sitemesh模板中使用Struts标记。

        要使用Sitemesh需要包含Freemark,Sitemesh和Sitemesh插件库文件。

配置过滤器

如果需要使用Freemark模板文件作为装饰器文件,需要在web.xml文件中添加如下配置:

xml 代码
  1. <filter>  
  2.     <filter-name>struts-cleanupfilter-name>  
  3.     <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUpfilter-class>  
  4. filter>  
  5. <filter>  
  6.     <filter-name>sitemeshfilter-name>  
  7.     <filter-class>org.apache.struts2.sitemesh.FreeMarkerPageFilterfilter-class>  
  8. filter>  
  9. <filter>  
  10.     <filter-name>strutsfilter-name>  
  11.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>  
  12. filter>  
  13.   
  14. <filter-mapping>  
  15.     <filter-name>struts-cleanupfilter-name>  
  16.     <url-pattern>/*url-pattern>  
  17. filter-mapping>  
  18. <filter-mapping>  
  19.     <filter-name>sitemeshfilter-name>  
  20.     <url-pattern>/*url-pattern>  
  21. filter-mapping>  
  22. <filter-mapping>  
  23.     <filter-name>strutsfilter-name>  
  24.     <url-pattern>/*url-pattern>  
  25. filter-mapping>  

        注意ActionContextCleanUp过滤器必须在FilterDispatcher之前配置,ActionContextCleanUp的主要 功能是通知FilterDispatcher执行完毕不要清除ActionContext,以便sitemesh装饰器可以访问Struts值堆栈。

配置装饰器

        在WEB-INF目录下创建一个decorator.xml文件,指定装饰器需要匹配哪些文件,下述示例指定main.flt将装饰所有的jsp文件:

xml 代码
  1. <!--sp-->xml version="1.0" encoding="ISO-8859-1"?>  
  2.   
  3. <decorators defaultdir="/decorators">  
  4.     <!-- Any urls that are excluded will never be decorated by Sitemesh -->  
  5.     <excludes>  
  6.         <pattern>/exclude.jsppattern>  
  7.         <pattern>/exclude/*pattern>  
  8.     excludes>  
  9.   
  10.     <decorator name="main" page="main.ftl">  
  11.         <pattern>/*.jsppattern>  
  12.     decorator>  
  13. decorators>  

        如果需要自定义装饰器映射器,需要在WEB-INF目录下创建一个sitemesh.xml文件(通常从发布包中拷贝过来更改相应部分)。这一步骤是可选 的,通常缺省的配置就能够满足要求。

定义装饰器文件

        缺省情况下,sitemesh假定装饰器文件保存在应用上下文根路径下的decorators目录下,如果采用如上配置,装饰器文件应该是ftl格式,如 果需要使用其他格式,需要更改过滤器配置。

访问被装饰页面

        在Freemark装饰器文件中,可以通过如下变量访问被装饰页面的相关部分:

xml 代码
  1. ${title}......访问被装饰页面的标题。   
  2.   
  3. ${head}......访问被装饰页面的头信息,标题除外。   
  4.   
  5. ${body}......访问被装饰页面的body内容。   
  6.   
  7. ${page.properties.meta.author}......访问被装饰页面的属性。   

内部变量

Freemark和Struts整合提供如下内部变量:

xml 代码
  1. stack......值堆栈本身,示例${stack.findString('ognl expr')}   
  2.   
  3. action......Action实例   
  4.   
  5. response/res......响应对象   
  6.   
  7. request/req......请求对象   
  8.   
  9. session......会话对象   
  10.   
  11. application......ServletContext   
  12.   
  13. base......请求的上下文路径   
 

下面介绍访问应用程序各范围属性的语法示例:

Application范围

假定Application范围有一个属性 myApplicationAttribute :

java 代码
  1. <#if Application.myApplicationAttribute?exists>   
  2.      ${Application.myApplicationAttribute}   
  3. if>  

java 代码
  1. <@s.property value="%{#application.myApplicationAttribute}" />  
Session范围

假定会话范围内有一个属性mySessionAttribute:

java 代码
  1. <#if Session.mySessionAttribute?exists>   
  2.      ${Session.mySessionAttribute}   
  3. if>  

java 代码
  1. <@s.property value="%{#session.mySessionAttribute}" />   
Request范围

假定请求范围有一个属性myRequestAttribute

java 代码
  1. <#if Request.myRequestAttribute?exists>   
  2.       ${Request.myRequestAttribute}   
  3. if>  

java 代码
  1. <@s.property value="%{#request.myRequestAttribute}" />  
Request参数

假定请求参数myParameter

java 代码
  1. <#if Parameters.myParameter?exists>   
  2.      ${Parameters.myParameter}   
  3. if>  

or

java 代码
  1. <@s.property value="%{#parameters.myParameter}" />  
Context参数

假定框架上下文有一参数myContextParam

java 代码
  1. ${stack.findValue('#myContextParam')}  

java 代码
  1. <@s.property value="%{#myContextParam}" />   
评论

相关推荐

Global site tag (gtag.js) - Google Analytics