当前位置: 移动技术网 > IT编程>开发语言>Java > Struts2拦截器 关于解决登录的问题

Struts2拦截器 关于解决登录的问题

2019年07月19日  | 移动技术网IT编程  | 我要评论

台湾娱乐新闻,昏厥的蒂法2,一百大吉他手

拦截器的工作原理如图 拦截器是由每一个action请求(request)都包装在一系列的拦截器的内部,通过redirectaction再一次发送请求。

拦截器可以在action执行直线做相似的操作也可以在action执行直后做回收操作。

我们可以让每一个action既可以将操作转交给下面的拦截器,action也可以直接退出操作返回客户既定的画面。

接下来我们该如何定义一个拦截器:

自定义一个拦截器如下:

1、实现interceptor接口或者继承abstractinterceptor抽象类。

2、创建一个struts.xml文件进行定义拦截器。

3、在需要使用的action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器(<default-interceptor-ref name="mystack"/>),

这样在不加特殊声明的情况下所有的action都被这个拦截器拦截<param name="excludemethods">loginview,login</param>。

①interceptor接口声明三个方法:

public class logininterceptor implements interceptor {

 private map<string,object> session = null;
 public void destroy() { }
 public void init() { }
 public string intercept(actioninvocation actioninvocation) throws exception {
 8     object myaction = actioninvocation.getaction();
  if(myaction instanceof useraction){
   system.out.println("你访问的action是useraction,不要校验session,否则死循环");
   //放行
   return actioninvocation.invoke();
  }else{
   system.out.println("你访问的action是:"+myaction);
  }

  session = actioncontext.getcontext().getsession();
  object user = session.get("user");
  if (user!=null){
   return actioninvocation.invoke();
  }else{
   return "login";
  }

}

注:该方法可以不加:<param name="excludemethods">loginview,login</param>

②让它继承 methodfilterinterceptor:

public class logininterceptor extends methodfilterinterceptor {
 private map<string,object> session = null;
 protected string dointercept(actioninvocation actioninvocation) throws exception {
  /*
  object myaction = actioninvocation.getaction();
  if(myaction instanceof useraction){
   system.out.println("你访问的action是useraction,不要校验session,否则死循环");
   //放行
   return actioninvocation.invoke();
  }else{
   system.out.println("你访问的action是:"+myaction);
  }
  */
  session = actioncontext.getcontext().getsession();
  object user = session.get("user");
  if (user!=null){
   return actioninvocation.invoke();
  }else{
   return "login";
  }
 }
}

③useraction继承actionsupport 实现 modeldriven<user>和sessionaware:

public class useraction extends actionsupport implements modeldriven<user>,sessionaware{

 private map<string,object> session = null;
 private user user = null;
   //驱动模型
 public user getmodel() {
  this.user = new user();
  return this.user;
 }

 public void setsession(map<string, object> map) {
  this.session = map;
 }

 public string loginview(){
  return "loginviewsuccess";
 }

 public string login(){
  if ("admin".equals(user.getusername())&&"123456".equals(user.getuserpassword())){
   session.put("user",user);
   return this.success;
  }else{
   return this.error;
  }

 }
}

struts.xml文件中:

<struts>
 <package name="mypackage" extends="struts-default">

  <interceptors>

   <interceptor name="logininterceptor" class="com.nf.action.logininterceptor"></interceptor>
   <interceptor-stack name="mystack">
    <interceptor-ref name="logininterceptor">
     <!--excludemethods需要生效的话,自定义的拦截器,不能使用实现interceptor接口,而是extends methodfilterinterceptor-->
     <param name="excludemethods">loginview,login</param><!--不用此行时 我们可以配合①使用拦截器-->
    </interceptor-ref>
    <interceptor-ref name="defaultstack"></interceptor-ref>
   </interceptor-stack>
  </interceptors>
  <!--配置一个默认拦截器,也就是所有的action都必须使用-->
  <default-interceptor-ref name="mystack"/>
  <global-results>
   <result name="login" type="redirectaction">useraction_loginview</result>
  </global-results>
  <!--不写method,默认就是execute-->
  <action name="indexaction" class="com.nf.action.indexaction" method="execute">
   <result name="success">/web-inf/jsp/index.jsp</result>
   <!--
   <interceptor-ref name="mystack"></interceptor-ref>
   -->
   <!--注释这里也可以放该代码 只不过每一个action都要放比较麻烦 
   <interceptor-ref name="logininterceptor"></interceptor-ref>
   <interceptor-ref name="defaultstack"></interceptor-ref>
   -->
  </action>

  <action name="otherfunctionaction" class="com.nf.action.otherfunctionaction">
   <!--不写name,默认就是success-->
   <result>/web-inf/jsp/otherfunction.jsp</result>
  </action>
  
  <action name="useraction_*" class="com.nf.action.useraction" method="{1}">
   <result name="loginviewsuccess">/web-inf/jsp/loginview.jsp</result>
   <result name="error">/web-inf/jsp/error.jsp</result>
   <result name="success" type="redirectaction">indexaction</result>
   <allowed-methods>login,loginview</allowed-methods>
  </action>
 </package>
</struts>

其中,<param name="excludemethods">loginview,login</param> 配置的过滤方法,意思是拦截器对其中的方法不起作用。在我这里,loginview是跳转到登录页面的方法。

login 是验证用户名和密码的方法,在其中会将通过验证的用户名放入session中。

总结:

1.在struts2 中,所有的拦截器都会继承 interceptor 这个接口。

2.如果我们没有添加拦截器,struts2 会为我们添加默认拦截器。当然我们要是指定了拦截器,我们自己的拦截器就会取代默认的拦截器,

那么我们就不能享受默认拦截器提供的一些功能。所以,一般我会把默认拦截器也加上。

例如,在以上配置项中,action 里面再加上<interceptor-ref name="defaultstack"></interceptor-ref>

以上这篇struts2拦截器 关于解决登录的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网