您的位置:首页 > 编程语言 > C语言/C++

c++ primer第五版 练习8.4

2014-11-21 19:44 330 查看

概述

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

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

配置过滤器

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

xml 代码

<filter>  

    <filter-name>struts-cleanupfilter-name>  

    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUpfilter-class>  

filter>  

<filter>  

    <filter-name>sitemeshfilter-name>  

    <filter-class>org.apache.struts2.sitemesh.FreeMarkerPageFilterfilter-class>  

filter>  

<filter>  

    <filter-name>strutsfilter-name>  

    <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>  

filter>  

  

<filter-mapping>  

    <filter-name>struts-cleanupfilter-name>  

    <url-pattern>/*url-pattern>  

filter-mapping>  

<filter-mapping>  

    <filter-name>sitemeshfilter-name>  

    <url-pattern>/*url-pattern>  

filter-mapping>  

<filter-mapping>  

    <filter-name>strutsfilter-name>  

    <url-pattern>/*url-pattern>  

filter-mapping>  

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

配置装饰器

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

xml 代码

<!--sp-->xml version="1.0" encoding="ISO-8859-1"?>  

  

<decorators defaultdir="/decorators">  

    <!-- Any urls that are excluded will never be decorated by Sitemesh -->  

    <excludes>  

        <pattern>/exclude.jsppattern>  

        <pattern>/exclude/*pattern>  

    excludes>  

  

    <decorator name="main" page="main.ftl">  

        <pattern>/*.jsppattern>  

    decorator>  

decorators>  

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

定义装饰器文件

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

访问被装饰页面

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

xml 代码

${title}......访问被装饰页面的标题。   

  

${head}......访问被装饰页面的头信息,标题除外。   

  

${body}......访问被装饰页面的body内容。   

  

${page.properties.meta.author}......访问被装饰页面的属性。   

内部变量

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

xml 代码

stack......值堆栈本身,示例${stack.findString('ognl expr')}   

  

action......Action实例   

  

response/res......响应对象   

  

request/req......请求对象   

  

session......会话对象   

  

application......ServletContext   

  

base......请求的上下文路径   

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

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

java 代码

<#if Application.myApplicationAttribute?exists>   
     ${Application.myApplicationAttribute}   
if>  




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


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

java 代码

<#if Session.mySessionAttribute?exists>   
     ${Session.mySessionAttribute}   
if>  




java 代码

<@s.property value="%{#session.mySessionAttribute}" />   

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

java 代码

<#if Request.myRequestAttribute?exists>   
      ${Request.myRequestAttribute}   
if>  




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


Request参数
假定请求参数myParameter

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


or

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


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

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




java 代码

<@s.property value="%{#myContextParam}" />   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: