Struts2拦截器之AliasInterceptor

这个拦截器在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请求参数里找。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts2 中,可以使用拦截器来实现权限控制。具体来说,可以编写一个自定义的拦截器,然后在 struts.xml 配置文件中将其配置为需要进行权限控制的 Action 的拦截器。以下是一个简单的权限拦截器示例: ```java public class AuthInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { // 获取当前用户 User user = (User) ActionContext.getContext().getSession().get("user"); // 判断用户是否拥有权限 if (user != null && user.hasPermission(invocation.getInvocationContext().getName())) { // 如果有权限,则继续执行 Action return invocation.invoke(); } else { // 如果没有权限,则跳转到错误页面 return "error"; } } } ``` 在上述代码中,我们首先获取了当前用户,然后判断用户是否拥有执行当前 Action 的权限。如果有权限,则继续执行 Action;否则,跳转到错误页面。 接下来,在 struts.xml 配置文件中,我们可以将该拦截器配置为需要进行权限控制的 Action 的拦截器,如下所示: ```xml <action name="myAction" class="com.example.MyAction"> <interceptor-ref name="authInterceptor"/> <result name="success">/myAction.jsp</result> <result name="error">/error.jsp</result> </action> <interceptor-stack name="authStack"> <interceptor-ref name="authInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> <interceptors> <interceptor name="authInterceptor" class="com.example.AuthInterceptor"/> </interceptors> ``` 在上述代码中,我们首先定义了一个名为 authStack 的拦截器栈,该拦截器栈包含了 authInterceptor 和 defaultStack 两个拦截器。然后,我们将 myAction Action 配置为使用 authStack 拦截器栈。最后,我们定义了一个名为 authInterceptor拦截器,并将其添加到了 interceptors 中。 这样一来,当用户访问 myAction Action 时,就会先执行 authInterceptor 拦截器,进行权限控制,然后再执行 defaultStack 拦截器栈中的其它拦截器。如果 authInterceptor 拦截器返回了 error,则会跳转到 error.jsp 页面;否则,会跳转到 myAction.jsp 页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值