Interceptor

Interceptor

拦截器是可以控制权限,当用户需要查看查看某些功能的时候,需要判断是不是登录了,如果没有登录的,就可拦截的过程。。

首先,我们都知道struts.xml 中有action 节点, 这个节点表示你想要访问的功能。

如果我们不想让用户直接去干遇到它,则可以为他设置一个拦截器iterceptor

可以再sturts.xml 中这样写:

复制代码
 <interceptors>
        <!--配置拦截器  -->
            <interceptor name="myinter" class="cn.happy.interceptor.MyInterceptor"></interceptor>
            <!--配置拦截器的栈  -->
            <interceptor-stack name="myStack">
            <!-- 配合拦截器引用 -->
               <interceptor-ref name="defaultStack"></interceptor-ref>
               <interceptor-ref name="myinter"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <!--默认拦截器的引用  -->
        <default-interceptor-ref name="myStack"/>
复制代码

现在有了这个拦截器,我们要实现的功能就可以通过被拦截器拦截了

现在比如说我们要写一个查看图书的功能。

首先创建出一个bookAction,这是里边包含我们要实现的功能用方法list()表示

1
2
3
4
5
6
7
8
9
10
package cn.happy.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class BookAction extends ActionSupport{
   public String list(){
       System.out.println("BookAction====list"); 
       return "list";
   }
}

 然后把这个action 关联到struts.xml中:在这里我们在struts.xml中的基础上再次创建出book.xml(这个xml是继承了struts.xml中包)

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
   <package name="book" namespace="/book" extends="main">
      <!-- 图书的配置 -->
         <action name="bookAction" class="cn.happy.action.BookAction" method="list">
            <result name="list">/myinter/list.jsp</result>
             <result name="success">/myinter/success.jsp</result>
              <result name="login">/myinter/login.jsp</result>
        </action>
   </package>
</struts>
复制代码

现在就到了关键的阶段,就是用于拦截的我们创建的拦截的类首先注意的是需要继承abstractInterceptor 或者实现Interceptor

关键代码如下:

复制代码
package cn.happy.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 拦截器
 * @author Happy
 *
 */
public class MyInterceptor implements Interceptor{

    
    public String intercept(ActionInvocation invocation) throws Exception {
         System.out.println("拦截器执行了===="+invocation.getProxy().getMethod());
         //01.获取Session对象
         Map<String, Object> session = ActionContext.getContext().getSession();
         Object obj = session.get("uname");
         String value=""; //代表Action执行完后的逻辑视图名
         //先获取到action请求的名称
         String actionName = invocation.getProxy().getActionName();
         //默认在登录页面  正在登录,登录成功之后需要放行
         if (actionName.equals("loginAction")) {
            invocation.invoke();
         }else {
             //判断是不是登录过了
             if(obj!=null){
                 //证明用户登录了
                 //放行      请求执行其他拦截器 后者具体的Action
                 value= invocation.invoke();
             }else {
                 //证明用户没有登录
                 value="login";
            }
         }
         System.out.println("逻辑视图"+value);
        return value;
    }
    /**
     * 销毁:执行一次
     */
    public void destroy() {
         System.out.println("拦截器destroy");
    }
    /**
     * 初始化一次:
     */
    public void init() {
         System.out.println("拦截器init");
    }

    
}
复制代码
原文地址:https://www.cnblogs.com/lianceng/p/5959192.html