Struts2 拦截器的使用,验证是否已经登陆

Struts2 拦截器的使用,验证是否已经登陆。

第一步:编写拦截器类,继承AbstructInterceptor 重新Intercept()。

    1.调用下一个拦截器或action之前做了什么。

    2.调用下一个拦截器或者action。

    3.调用拦截器或action之后做了什么。

代码:

[java]  view plain  copy
  1. public class LoginInterceptor extends AbstractInterceptor{  
  2.   
  3.     @Override  
  4.     public String intercept(ActionInvocation invocation) throws Exception {  
  5.         String result = "";  
  6.             System.out.println("before invoke");  
  7.             //因为要把这个拦截器设置成默认拦截器,所以如果是登陆的action则跳过。  
  8.             if(MyAction.class==invocation.getAction().getClass()){  
  9.                 return invocation.invoke();  
  10.             }  
  11.             //取session中的username,如果等于null则没有登陆,返回input跳转到登陆页面  
  12.         if(ServletActionContext.getRequest().getSession().getAttribute("username")==null){  
  13.                 return "input";  
  14.             }  
  15.         result = invocation.invoke();  
  16.             System.out.println("after invoke");  
  17.         return result;  
  18.     }  

第二步:在struts.xml中定义拦截器。

[html]  view plain  copy
  1. <interceptors>  
  2.     <interceptor name="loginInterceptor" class="com.interceptor.LoginInterceptor"></interceptor>  
  3.             <interceptor-stack name="myStack">  
  4.                 <interceptor-ref name="loginInterceptor"></interceptor-ref>  
  5.                 <interceptor-ref name="defaultStack"></interceptor-ref>  
  6.             </interceptor-stack>  
  7. </interceptors>  

设置成默认的拦截器

[html]  view plain  copy
  1. <default-interceptor-ref name="myStack"></default-interceptor-ref>  


如果不需要设置成默认拦截器,则在具体的action中配置

[html]  view plain  copy
  1. <action name="login" class="com.action.LoginAction">  
  2.    <interceptor-ref name="myStack"></interceptor-ref>  
  3. ......  


登陆提交到的action里的login()

[java]  view plain  copy
  1. public String login() throws Exception {  
  2.           
  3.         String  target = "";  
  4.         if(username.equals("hsp")&&password.equals("hsp")){  
  5.             target = "success";  
  6.             this.getSession().setAttribute("username", username);  
  7.         }  
  8.         else{  
  9.             target = "input";  
  10.         }  
  11.         return target;  
  12.     }  


login.jsp代码

[html]  view plain  copy
  1. <body>  
  2.   <h2>登录</h2>  
  3.   <form action="myAction!login.action" method="post">  
  4.     username:<input type="text" name="username"/><br/>  
  5.     password:<input type="password" name="password"/><br/>  
  6.     <input type="submit" value="login"><br/>  
  7.   </form>  
  8.  </body>  


struts.xml代码

[html]  view plain  copy
  1. <action name="myAction" class="com.action.MyAction">  
  2.             <result name="success" type="redirectAction">welcom</result>  
  3.               
  4.         </action>  
  5.         <action name="welcom">  
  6.             <result>welcom.jsp</result>  
  7.         </action>  


当用户正确登陆后跳转到welcom.action。复制url再把浏览器关闭。重新打开浏览器,请求复制的url。拦截器会起作用,让页面跳转的登陆页面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值