Struts 2 interceptor interface: interceptor

Syntax:

public interface Interceptor extends Serializable{
  void destroy();
  void init();
  String intercept(ActionInvocation invocation)throws Exception;
}

Example

public class TestAction extends ActionSupport{
  private static final long serialVersionUID = 1L;
  public String execute()throws Exception{  //线程睡眠1秒
    Thread.sleep(1000);
    return SUCCESS;
  }
}
<struts>
  <!--声明常量(开发模式)-->
  <constant name="struts.devMode"value="true"/>
  <!--声明常量(在Struts2的配置文件修改后,自动加载)-->
  <constant name="struts.configuration.xml.reload"value="true"/>
  <!--声明包-->
  <package name="myPackge"extends="struts-default">
    <!--配置Action-->
    <action name="testAction"class="com.lyq.action.TestAction">
      <!--配置拦截器-->
      <interceptor-ref name="timer"/>
      <!--配置返回页面-->
      <result>success.jsp</result>
    </action>
  </package>
</struts>

Typical application

/**
*验证用户登录的拦截器
*@authorZX
*/
public class LoginInterceptor extends MethodFilterInterceptor{
  private static final long serialVersionUID = 1L;
  @Override
    protected String doIntercept(ActionInvocation invocation)throws Exception{  //获取ActionContext
    ActionContext context = invocation.getInvocationContext();//获取Map类型的session
    Map<String,Object>session=context.getSession();  //判断用户是否登录
    if(session.get("user")!=null){  //调用执行方法
      return invocation.invoke();
    }  //返回登录
    return Action.LOGIN;
  }
}
/**
*用户Action
*@author ZX
*/
public class UserAction extends ActionSupport{
  private static final long serialVersionUID = 1L;  //用户名
  private String username;  //密码
  private String password;  //获取Map类型的session变量
  Map<String,Object>session = ActionContext.getContext().getSession();//登录方法
  public String login()throws Exception{  //验证用户名
    if("admin".equalsIgnoreCase(username)&&"admin".equalsIgnoreCase(password)){  //登录成功,将用户名放置到sesseion之中
      session.put("user",username);  //返回管理页面
      return"main";
    }  //返回登录页面
    return LOGIN;
  }  //注销用户
  public String logoff()throws Exception{  //逐出session中的变量
    session.remove("user");  //返回登录页面
    return LOGIN;
  }  //进入管理页面
  public String main()throws Exception{
    return SUCCESS;
  }
  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;
  }
}
<struts>
  <!--声明常量(开发模式)-->
  <constant name="struts.devMode"value="true"/>
  <!--声明常量(在Struts2的配置文件修改后,自动加载)-->
  <constant name="struts.configuration.xml.reload"value="true"/>
  <!--声明包-->
  <package name="myPackge"extends="struts-default">
    <!--配置拦截器-->
    <interceptors>
      <!--验证用户登录的拦截器-->
      <interceptor name="loginInterceptor"class="com.lyq.action.LoginInterceptor"/>
    </interceptors>
    <!--配置UserAction-->
    <action name="userAction"class="com.lyq.action.UserAction">
      <!--配置返回页面-->
      <result>/main.jsp</result>
      <result name="login">/login.jsp</result>
      <result name="main"type="redirectAction">userAction!main</result>
      <!--添加验证用户登录的拦截器-->
      <interceptor-ref name="loginInterceptor">
        <param name="excludeMethods">login,logoff</param>
      <param name="includeMethods">main</param>
    </interceptor-ref>
  <!--添加默认拦截器栈-->
  <interceptor-ref name="defaultStack"/>
  </action>
</package>
</struts>
<body>
  <s:form action="userAction" method="post">
    <s:textfield name="username" label="用户名"></s:textfield>
    <s:password name="password" label="密码"></s:password>
    <s:submit value="登录" method="login"></s:submit>
  </s:form>
  <s:a href="https://www.jb51.ccindex.jsp">返回首页</s:a>
</body>
<body>
  <h1>
    管理页面
  </h1>
  <s:a action="userAction" method="logoff">退出</s:a>
  <s:a href="https://www.jb51.ccindex.jsp">返回首页</s:a>
</body>
<body>
  <s:a action="userAction" method="main">进入管理页面</s:a>
  <s:a href="https://www.jb51.cclogin.jsp">登录</s:a>
</body>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>