您的位置:首页 > 其它

<param name="aliases"></param>拦截器的参数

2011-11-10 10:53 399 查看
这个拦截器在xwork的jar包里,它的作用是给参数起一个别名,可用于在action链中以不同的名字共享同一个参数,也可用于把http请求参数以不同的名字映射到action里。

拦截器有一个参数:aliasesKey,可通过在struts.xml中定义该拦截器时指定其值,默认值是aliases,表示一个别名的map。

下面以实现在action链中以不同的名字共享同一个参数为例:

struts.xml

<package name="test" namespace="/test">

<action name="aliasTestFrom" class="test.AliasInterceptorTestFromAction">

<result type="chain">aliasTestTo</result>

</action>

<action name="aliasTestTo" class="test.AliasInterceptorTestToAction">

<param name="aliases">#{'someList' : 'otherList'}</param>

<result>/test/alias_result.jsp</result>

</action>

</package>

param标签的name属性值应该和拦截器参数aliasesKey的值一样,这样拦截器才知道你是否指定了action的别名map。这个map应该象#{原参数名1 : 别名1, 原参数名2 : 别名2}这样的形式,这是一个定义map的OGNL表达式。

该拦截器已经包含在defaultStack中,因此上面没有显示的指定。

相应的,在本例中,test.AliasInterceptorTestFromAction里有一个字段someList:

AliasInterceptorTestFromAction.java

private List someList;

public List getSomeList() {

return someList;

}

public void setSomeList(List someList) {

this.someList = someList;

}

test.AliasInterceptorTestToAction里有一个字段otherList:

AliasInterceptorTestToAction.java

private List otherList;

public List getOtherList() {

return otherList;

}

public void setOtherList(List otherList) {

this.otherList = otherList;

}

注意:在AliasInterceptorTestToAction中不能有一个叫someList的字段,如果有的话,otherList最终的值将是AliasInterceptorTestToAction中的someList,而非AliasInterceptorTestFromAction的值。因为该拦截器的实现是在action的值栈里找到原参数名后设置给别名,如果两个action都有someList,而AliasInterceptorTestToAction位于栈顶,它的someList将被赋给otherList,这并不是我们所期望的结果。

很重要:注意::!!!!!someList也可以来自于http请求参数,因为拦截器如果在action的值栈里没有找到someList,还会在http请求参数里找。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐