jimphei学习工作室

jimphei学习工作室

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  23 随笔 :: 0 文章 :: 1 评论 :: 0 Trackbacks

2009年11月23日 #

import java.util.Map;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.ui.velocity.VelocityEngineUtils;

public class MsgBean ...{
    private VelocityEngine velocityEngine;

    private String msg;

    private Map model; // 用来保存velocity中的参数值

    private String encoding; // 编码

    private String templateLocation; // 注入的velocity模块

    public String getEncoding() ...{
        return encoding;
    }

    public void setEncoding(String encoding) ...{
        this.encoding = encoding;
    }

    public String getTemplateLocation() ...{
        return templateLocation;
    }

    public void setTemplateLocation(String templateLocation) ...{
        this.templateLocation = templateLocation;
    }

    public Map getModel() ...{
        return model;
    }

    public void setModel(Map model) ...{
        this.model = model;
    }

    public String getMsg() ...{
        // return title;
        // 将参数值注入到模块后的返回值
        return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                templateLocation, encoding, model);

    }

    public void setMsg(String msg) ...{
        this.msg = msg;
    }

    public VelocityEngine getVelocityEngine() ...{
        return velocityEngine;
    }

    public void setVelocityEngine(VelocityEngine velocityEngine) ...{
        this.velocityEngine = velocityEngine;
    }

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 

   
 <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> 
   <property name="resourceLoaderPath">
            <value>classpath:velocity</value>
     </property>
    <property name="velocityProperties">
                 <props>
                       <prop key="resource.loader">class</prop>
                       <prop key="class.resource.loader.class">
                             org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                       </prop>
                       <prop key="velocimacro.library"></prop>
                       <prop key="input.encoding">GBK</prop>
                       <prop key="output.encoding">GBK</prop>
                       <prop key="default.contentType">text/html; charset=GBK</prop>
                 </props>
           </property>
</bean>

<bean id="msgBean" class="MsgBean">
        <property name="templateLocation" value="test.vm"></property>
        <property name="encoding" value="GBK"></property>
        <property name="velocityEngine" ref="velocityEngine"></property>
</bean>


</beans>

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestVeloctiy ...{
    public static void main(String[] args) ...{
        // TODO Auto-generated method stub
        ApplicationContext ctx=new ClassPathXmlApplicationContext("test3.xml");
        MsgBean    msgBean=((MsgBean)ctx.getBean("msgBean"));
        Map<String, String> data = new HashMap<String, String>();
        data.put("me","yourname");
        msgBean.setModel(data);
        System.out.println(msgBean.getMsg());
       
        
        //根据apache common IO 组件直接将内容写到一个文件中去.
         File dest = new File( "test.html" );         
          try ...{
            FileUtils.writeStringToFile( dest, msgBean.getMsg(), "GBK" );
        } catch (IOException e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pengchua/archive/2008/01/17/2049490.aspx

posted @ 2009-11-26 11:36 jimphei 阅读(1133) | 评论 (0)编辑 收藏

引用自:http://blog.csdn.net/axzywan/archive/2008/07/12/2643921.aspx

取Session中的值

<c:out value="${sessionScope.user.userId}"></c:out><br>  

<c:out value="${user.userLoginName}"></c:out><br>    

<s:property value="#session.user.userId"/><br>  

${session.user.userId}<br> 

${sessionScope.user.userId}<br>

OGNL

OGNL 是Object Graph Navigation Language 的简称,详细相关的信息可以参考:http://www.ognl.org 。这里我们只涉及Struts2 框架中对OGNL 的基本支持。

 

OGNL 是一个对象,属性的查询语言。在OGNL 中有一个类型为Map 的Context (称为上下文),在这个上下文中有一个根元素(root ),对根元素的属性的访问可以直接使用属性名字,但是对于其他非根元素属性的访问必须加上特殊符号# 。

 

在Struts2 中上下文为ActionContext ,根元素位Value Stack (值堆栈,值堆栈代表了一族对象而不是一个对象,其中Action 类的实例也属于值堆栈的一个)。ActionContext 中的内容如下图:

              |

              |--application

              |

              |--session

context map---|

               |--value stack(root)

              |

              |--request

              |

              |--parameters

              |

              |--attr (searches page, request, session, then application scopes)

              |

因为Action 实例被放在Value Stack 中,而Value Stack 又是根元素(root )中的一个,所以对Action 中的属性的访问可以不使用标记# ,而对其他的访问都必须使用# 标记。

 

引用Action 的属性

<s:property value="postalCode"/>

ActionContext 中的其他非根(root )元素的属性可以按照如下的方式访问:

<s:property value="#session.mySessionPropKey"/> or

<s:property value="#session["mySessionPropKey"]"/> or

<s:property value="#request["mySessionPropKey"]/>

 

Action 类可以使用ActionContext 中的静态方法来访问ActionContext 。

ActionContext.getContext().getSession().put("mySessionPropKey", mySessionObject);

 

OGNL 与Collection (Lists ,Maps ,Sets )

 

生成List 的语法为: {e1,e2,e3}.

<s:select label="label" name="name"

list="{'name1','name2','name3'}" value="%{'name2'}" />

上面的代码生成了一个HTML Select 对象,可选的内容为: name1 ,name2 ,name3 ,默认值为:name2 。

 

生成Map 的语法为:#{key1:value1,key2:value2}.

<s:select label="label" name="name"

list="#{'foo':'foovalue', 'bar':'barvalue'}" />

上面的代码生成了一个HTML Select 对象,foo 名字表示的内容为:foovalue ,bar 名字表示的内容为:barvalue 。

 

判断一个对象是否在List 内存在:

<s:if test="'foo' in {'foo','bar'}">

   muhahaha

</s:if>

<s:else>

   boo

</s:else>

 

<s:if test="'foo' not in {'foo','bar'}">

   muhahaha

</s:if>

<s:else>

   boo

</s:else>

 

取得一个List 的一部分:

?   –   所有满足选择逻辑的对象

^   -    第一个满足选择逻辑的对象

$   -    最后一个满足选择逻辑的对象

例如:

person.relatives.{? #this.gender == 'male'}

上述代码取得这个人(person )所有的男性(this.gender==male )的亲戚(relatives)

 

 

Lambda 表达式

 

OGNL 支持简单的Lambda 表达式语法,使用这些语法可以建立简单的lambda 函数。

 

例如:

Fibonacci:

if n==0 return 0;

elseif n==1 return 1;

else return fib(n-2)+fib(n-1);

fib(0) = 0

fib(1) = 1

fib(11) = 89

 

OGNL 的Lambda 表达式如何工作呢?

Lambda 表达式必须放在方括号内部,#this 表示表达式的参数。例如:

<s:property value="#fib =:[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)], #fib(11)" />

 

#fib =:[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)] 定义了一个Lambda 表达式,

#fib(11) 调用了这个表达式。

 

所以上述代码的输出为:89

 

在JSP2.1 中# 被用作了JSP EL (表达式语言)的特殊记好,所以对OGNL 的使用可能导致问题,

一个简单的方法是禁用JSP2.1 的EL 特性,这需要修改web.xml 文件:

<jsp-config>

    <jsp-property-group>

      <url-pattern>*.jsp</url-pattern>

      <el-ignored>true</el-ignored>

    </jsp-property-group>

</jsp-config>

关于EL表达式语言的简单总结
 

基本语法

一、EL简介
  1.语法结构
    ${expression}
  2.[]与.运算符
    EL 提供.和[]两种运算符来存取数据。
    当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要使用 []。例如:
        ${user.My-Name}应当改为${user["My-Name"] }
    如果要动态取值时,就可以用[]来做,而.无法做到动态取值。例如:
        ${sessionScope.user[data]}中data 是一个变量
  3.变量
    EL存取变量数据的方法很简单,例如:${username}。它的意思是取出某一范围中名称为username的变量。
    因为我们并没有指定哪一个范围的username,所以它会依序从Page、Request、Session、Application范围查找。
    假如途中找到username,就直接回传,不再继续找下去,但是假如全部的范围都没有找到时,就回传null。
    属性范围在EL中的名称
        Page         PageScope
        Request         RequestScope
        Session         SessionScope
        Application     ApplicationScope
       
二、EL隐含对象
  1.与范围有关的隐含对象
  与范围有关的EL 隐含对象包含以下四个:pageScope、requestScope、sessionScope 和applicationScope;
  它们基本上就和JSP的pageContext、request、session和application一样;
  在EL中,这四个隐含对象只能用来取得范围属性值,即getAttribute(String name),却不能取得其他相关信息。
 
  例如:我们要取得session中储存一个属性username的值,可以利用下列方法:
    session.getAttribute("username") 取得username的值,
  在EL中则使用下列方法
    ${sessionScope.username}

  2.与输入有关的隐含对象
  与输入有关的隐含对象有两个:param和paramValues,它们是EL中比较特别的隐含对象。
 
  例如我们要取得用户的请求参数时,可以利用下列方法:
    request.getParameter(String name)
    request.getParameterValues(String name)
  在EL中则可以使用param和paramValues两者来取得数据。
    ${param.name}
    ${paramValues.name}

  3.其他隐含对象
 
  cookie
  JSTL并没有提供设定cookie的动作,
  例:要取得cookie中有一个设定名称为userCountry的值,可以使用${cookie.userCountry}来取得它。

  header和headerValues
  header 储存用户浏览器和服务端用来沟通的数据
  例:要取得用户浏览器的版本,可以使用${header["User-Agent"]}。
  另外在鲜少机会下,有可能同一标头名称拥有不同的值,此时必须改为使用headerValues 来取得这些值。

  initParam
  initParam取得设定web站点的环境参数(Context)
  例:一般的方法String userid = (String)application.getInitParameter("userid");
    可以使用 ${initParam.userid}来取得名称为userid

  pageContext
  pageContext取得其他有关用户要求或页面的详细信息。
    ${pageContext.request.queryString}         取得请求的参数字符串
    ${pageContext.request.requestURL}         取得请求的URL,但不包括请求之参数字符串
    ${pageContext.request.contextPath}         服务的web application 的名称
    ${pageContext.request.method}           取得HTTP 的方法(GET、POST)
    ${pageContext.request.protocol}         取得使用的协议(HTTP/1.1、HTTP/1.0)
    ${pageContext.request.remoteUser}         取得用户名称
    ${pageContext.request.remoteAddr }         取得用户的IP 地址
    ${pageContext.session.new}             判断session 是否为新的
    ${pageContext.session.id}               取得session 的ID
    ${pageContext.servletContext.serverInfo}   取得主机端的服务信息

三、EL运算符
  1.算术运算符有五个:+、-、*或$、/或div、%或mod
  2.关系运算符有六个:==或eq、!=或ne、<或lt、>或gt、<=或le、>=或ge
  3.逻辑运算符有三个:&&或and、||或or、!或not
  4.其它运算符有三个:Empty运算符、条件运算符、()运算符
    例:${empty param.name}、${A?B:C}、${A*(B+C)}
 
四、EL函数(functions)。
  语法:ns:function( arg1, arg2, arg3 …. argN)
  其中ns为前置名称(prefix),它必须和taglib 指令的前置名称一置

---------------------------------------------

补充:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt " %>

FOREACH:

<c:forEach items="${messages}"
var="item"
begin="0"
end="9"
step="1"
varStatus="var">
……
</c:forEach>


OUT:

<c:out value="${logininfo.username}"/>
c:out>将value 中的内容输出到当前位置,这里也就是把logininfo 对象的
username属性值输出到页面当前位置。
${……}是JSP2.0 中的Expression Language(EL)的语法。它定义了一个表达式,
其中的表达式可以是一个常量(如上),也可以是一个具体的表达语句(如forEach循环体中
的情况)。典型案例如下:
Ø ${logininfo.username}
这表明引用logininfo 对象的username 属性。我们可以通过“.”操作符引
用对象的属性,也可以用“[]”引用对象属性,如${logininfo[username]}
与${logininfo.username}达到了同样的效果。
“[]”引用方式的意义在于,如果属性名中出现了特殊字符,如“.”或者“-”,
此时就必须使用“[]”获取属性值以避免语法上的冲突(系统开发时应尽量避免
这一现象的出现)。
与之等同的JSP Script大致如下:
LoginInfo logininfo =
(LoginInfo)session.getAttribute(“logininfo”);
String username = logininfo.getUsername();
可以看到,EL大大节省了编码量。
这里引出的另外一个问题就是,EL 将从哪里找到logininfo 对象,对于
${logininfo.username}这样的表达式而言,首先会从当前页面中寻找之前是
否定义了变量logininfo,如果没有找到则依次到Request、Session、
Application 范围内寻找,直到找到为止。如果直到最后依然没有找到匹配的
变量,则返回null.
如果我们需要指定变量的寻找范围,可以在EL表达式中指定搜寻范围:
${pageScope.logininfo.username}
${requestScope.logininfo.username}
${sessionScope.logininfo.username}
${applicationScope.logininfo.username}
在Spring 中,所有逻辑处理单元返回的结果数据,都将作为Attribute 被放
置到HttpServletRequest 对象中返回(具体实现可参见Spring 源码中
org.springframework.web.servlet.view.InternalResourceView.
exposeModelAsRequestAttributes方法的实现代码),也就是说Spring
MVC 中,结果数据对象默认都是requestScope。因此,在Spring MVC 中,
以下寻址方法应慎用:
${sessionScope.logininfo.username}
${applicationScope.logininfo.username}
Ø ${1+2}
结果为表达式计算结果,即整数值3。
Ø ${i>1}
如果变量值i>1的话,将返回bool类型true。与上例比较,可以发现EL会自
动根据表达式计算结果返回不同的数据类型。
表达式的写法与java代码中的表达式编写方式大致相同。

IF / CHOOSE:

<c:if test="${var.index % 2 == 0}">
*
</c:if>
判定条件一般为一个EL表达式。
<c:if>并没有提供else子句,使用的时候可能有些不便,此时我们可以通过<c:choose>
tag来达到类似的目的:
<c:choose>
<c:when test="${var.index % 2 == 0}">
*
</c:when>
<c:otherwise>
!
</c:otherwise>
</c:choose>
类似Java 中的switch 语句,<c:choose>提供了复杂判定条件下的简化处理手法。其
中<c:when>子句类似case子句,可以出现多次。上面的代码,在奇数行时输出“*”号,
而偶数行时输出“!”。
---------------------------------------------

再补充:

 1    EL表达式用${}表示,可用在所有的HTML和JSP标签中 作用是代替JSP页面中复杂的JAVA代码.

        2   EL表达式可操作常量 变量 和隐式对象. 最常用的 隐式对象有${param}和${paramValues}. ${param}表示返回请求参数中单个字符串的值. ${paramValues}表示返回请求参数的一组值.pageScope表示页面范围的变量.requestScope表示请求对象的变量. sessionScope表示会话范围内的变量.applicationScope表示应用范围的变量.

        3   <%@  page isELIgnored="true"%> 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁止.JSP2.0中默认的启用EL语言.

        4   EL语言可显示 逻辑表达式如${true and false}结果是false    关系表达式如${5>6} 结果是false     算术表达式如 ${5+5} 结果是10

        5   EL中的变量搜索范围是:page request session application   点运算符(.)和"[ ]"都是表示获取变量的值.区别是[ ]可以显示非词类的变量


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/stonec/archive/2009/10/09/4647394.aspx

posted @ 2009-11-23 10:53 jimphei 阅读(271) | 评论 (0)编辑 收藏