struts 登录拦截器的使用

 LoginAction

public class LoginAction extends ActionSupport implements SessionAware{

 private String username;
 private String password;
 private Map session;
 
 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String execute(){
  if(username.equals("zs")&&password.equals("123")){
   session.put("username", username);
   session.put("password", password);
   return SUCCESS;
  }
  return ERROR;
 }

 public void setSession(Map<String, Object> arg0) {
  
  this.session=arg0;
 }
}

LoginBeforeAction

import com.opensymphony.xwork2.ActionSupport;

public class LoginBeforeAction extends ActionSupport{

 public String execute(){
  return SUCCESS;
 }
}

LoginAfterAction

public class LoginAfterAction extends ActionSupport implements SessionAware{

 private String username;
 private String password;
 private Map session;
 
 
 
 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String execute(){
  return SUCCESS;
 }

 public void setSession(Map<String, Object> arg0) {
  
  this.session=arg0;
 }
}

LoginIntecepter

public class LoginIntecepter extends AbstractInterceptor{

 public String intercept(ActionInvocation invocation) throws Exception {
  Map session=(Map)invocation.getInvocationContext().getSession();
  Object object=session.get("username");
  if(object!=null){
   return invocation.invoke();
  }
  return "error";
 }

}

 

<struts>
    <package name="login" namespace="/login" extends="struts-default">
    <interceptors>
     <interceptor name="loginIntercepter" class="com.login.actions.LoginIntecepter"></interceptor>
     <interceptor-stack name="mystack">
      <interceptor-ref name="defaultStack"></interceptor-ref>
      <interceptor-ref name="loginIntercepter"></interceptor-ref>
     </interceptor-stack>
    </interceptors>
     <action name="before" class="com.login.actions.LoginBeforeAction">
      <result>/login.jsp</result>
     </action>
     <action name="login" class="com.login.actions.LoginAction">
      <result>/main.jsp</result>
      <result name="error">/login.jsp</result>
     </action>
     <action name="after" class="com.login.actions.LoginAfterAction">
      <result>/main.jsp</result>
      <result name="error">/login.jsp</result>
      <interceptor-ref name="mystack"></interceptor-ref>
     </action>
    </package>
 
</struts>

 <form action="login/login.do" method="post">
    username:<input type="text" name="username"/><p>
    password:<input type="password" name="password"/><p>
    <input type="submit" value="submit"/>
   
    </form>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值