sitemesh velocity freemarker struts2

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

sitemesh velocity:

http://www.opensymphony.com/sitemesh/velocity-decorators.html

sitemesh freemarker:

http://www.opensymphony.com/sitemesh/freemarker-decorators.html

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> 

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

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

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

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

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

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

java 代码

 

Request参数

假定请求参数myParameter:

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

or

java 代码:

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

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

${stack.findValue('#myContextParam')}  
 

or

java 代码:

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

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

${stack.findValue('#myContextParam')}  

 

or

java 代码:

   1.  <@s.property value="%{#myContextParam}" />   

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值