现我已实现了可拦截所有action,但我并不想所有的action都被拦截,或者说我想某个action中的某个方法不被拦截,
如何实现呢?比如,我有个action,示例代码如下:
public class UserAction extends ActionSupport {
public String login() throws Exception{
          //.....登录
           ActionContext.getContext().getSession().put("user", user);//通过user判断是否登录
           return SUCCESS;
         }
public String other() throws Exception {
          //.....其它
           return SUCCESS;
         }
}
当执行login方法时,不要拦截,当other时,才拦截,现在我是两个都拦截的,怎么办呢?
ActionContext.getContext().getSession().put("user", user);只在login中设置,而这也被拦截了,
那拦截器中的user不是永远为空的了?即永远登录不了了,可能我理解拦截器有偏差,请高人请教下吧,谢了!

解决方案 »

  1.   

    你是通过什么来指定程序执行哪一个方法的?
    例如url指定:....?method=login。是这样的话就先得到url中method的值,如果是login的话就在拦截器中做个判断跳过不拦截。
    关键是你怎样指定执行哪个action方法,就在哪里做判断。
      

  2.   

    拦截器类继承MethodFilterInterceptor 重写doIntercept方法
    在struts.xml中
    定义拦截器 
    <action >
    <interceptor-ref name=定义拦截器>
        定义不被拦截的方法
        <param name="excludeMethods">login</param>
        定义被拦截的方法
        <param name="includeMethods">other</param>
    </action>