首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2 中#%跟$符号的用途(转载)

2012-11-03 
Struts2 中#、%和$符号的用途(转载)Struts2 中#、%和$符号的用途(转载)?#、%和$符号在OGNL表达式中经常出现,

Struts2 中#、%和$符号的用途(转载)

Struts2 中#、%和$符号的用途(转载)

?

#、%和$符号在OGNL表达式中经常出现,而这三种符号也是开发者不容易掌握和理解的部分。在这里笔者简单介绍它们的相应用途。

1.#符号的用途一般有三种。
?? 1)访问非根对象属性,例如示例中的#session.msg表达式,由于Struts 2中值栈被视为根对象,所以访问其他非根对象时,需要加#前缀。实际上,#相当于ActionContext. getContext();#session.msg表达式相当于ActionContext.getContext().getSession(). getAttribute(”msg”) 。

??? 2)用于过滤和投影(projecting)集合,如示例中的persons.{?#this.age>20}。

??? 3)用来构造Map,例如示例中的#{’foo1′:’bar1′, ’foo2′:’bar2′}。

2.%符号
%符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值。如下面的代码所示:
构造Map
<s:set name=”foobar” value=”#{’foo1′:’bar1′, ‘foo2′:’bar2′}” />

<p>The value of key “foo1″ is <s:property value=”#foobar['foo1']” /></p>

<p>不使用%:<s:url value=”#foobar['foo1']” /></p>

<p>使用%:<s:url value=”%{#foobar['foo1']}” /></p>

?

3.$符号
$符号主要有两个方面的用途。

?? 在国际化资源文件中,引用OGNL表达式,例如国际化资源文件中的代码:reg.agerange=国际化资源信息:年龄必须在${min}同${max}之间。

??? 在Struts 2框架的配置文件中引用OGNL表达式,例如下面的代码片断所示:

<validators>

??? <field name=”intb”>

??????????? <field-validator type=”int”>

??????????? <param name=”min”>10</param>

??????????? <param name=”max”>100</param>

??????????? <message>BAction-test校验:数字必须为${min}为${max}之间!</message>

??????? </field-validator>

??? </field>

****************************************************************

看了这个之后就试了改为value ="%{#session.username}" 奇怪它就真的成功了~~哈哈~

?

???? 访问值栈中Action的普通属性:userName --> <s:property value="userName" /><br />
???? 访问值栈中Action的对象的普通属性:user.userName | user['userName'] | user["userName"] --> <s:property value="user.userName" /> | <s:property value="user['userName']" /> | <s:property value="user["userName"]" /><br />
???? 访问值栈中Action的对象的普通属性:user.firend.userName --> <s:property value="user.firend.userName" /><br />
???? 访问值栈中Action的普通方法:test() --> <s:property value="test()" /><br />
???? 访问值栈中Action的属性的普通方法:userName.length() --> <s:property value="userName.length()" /><br />
???? 访问值栈中Action的对象的普通方法:user.hello() --> <s:property value="user.hello()" /><br />

?

随着注解越来越普遍的使用,struts2也开始支持注解,并宣称支持0配置(XML配置文件),我最近在使用Struts2框架,也顺便了解一了一下Struts2的注解配置.

????? 1.在Web.xml配置文件中添加

<filter>?
<filter-name>struts2</filter-name>?
??????? <filter-class>?
?????????????????? org.apache.struts2.dispatcher.FilterDispatcher?
???????? </filter-class>?
??? <init-param>?
??????? <param-name>actionPackages</param-name>?
??????? <param-value>com.learn.action</param-value>?
???? </init-param>?
</filter>
??????? 这样sturts2框架会自动扫描action包中的命名为*Action或者集成ActionSupport的类,例如一个类命名为SearchAction,那么Struts2框架会自动扫描它,认为它为一个Action类,并且将所有search.action的请求都转发给该类进行处理,这时我们又遇到了一个问题,如果这个Action是多Method的Action那么我们又该怎么办呢?因为不能像在XML中那样配置Method属性,这时我们可以在请求是加上!MethodName以请求特定的Method即可,例如想要请求SearchAction的search方法,我们可以直接这样请求:search!search.action.

?????? 以上讲到的是Struts2框架如何找到没有在XML中定义的Action,并且怎么样将请求转发给适当的Action来进行处理.

?????? 我们都知道在XML中可以定义Action处理完以后的跳转,同样利用Annotation也可以做到这一点:

??????? 2.Resules的Annotation定义

??????? 我们只需要在Action Class的类的前面加上@Result即可以定义Action的跳转

??????? 例如:

?

@Results({?
?? @Result(name="input" value="/input.jsp" type=NullResult.class),?
?? @Reuslt(name="success" value="/success.jsp" type=NullResult.class),?
?? @Result(name="error" value="/error.jsp" type=NullResult.class)?
})
??????? 上面是一个典型的多Result配置,name属性指定返回的字符串,value指定要跳转的页面,type指定Result的类型,type是一个很重要的属性,他有一下几种情况:

??????? 1)NullResult : 默认的可以省略

??????? 2)ActionChainResult : 用于从一个Action跳转到另外一个Action

??????? 例如:

?

@Result?
(name = "search",value= "search",type=ActionChainResult.class,?
params={"method","search"})
??????? 这个配置是如果返回值为"search"就跳转掉SearchAction的search方法,如果不指定params就跳转到SearchAction的execute方法

??????? 3)StreamResult:用于文件下载

??????? 例如:

?

@Result?
(name="success",value="inputStream",type=StreamResult.class,?
params={"bufferSize",FileConstant.DOWNLOAD_BUFFER_SIZE})
??????? 作为Struts2的Annotation配置中最重要的Result配置大致有这几种情况,关于Struts2 Annotation的其他配置我也不是十分了解,就以后在介绍吧!!!

?

热点排行