Struts2 自定义拦截器

1.自定义一个实现Interceptor的自定义拦截器类

package cn.gh.interceptor.myinter;

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

import java.util.Map;

/**
 * Created by guo on 2017/10/25.
 */
public class myinterseptor implements Interceptor {
    public void destroy() {

    }

    public void init() {
        System.out.println("自定义拦截器已启动--------");
    }

    public String intercept(ActionInvocation actionInvocation) throws Exception {
        System.out.println("对象"+actionInvocation);
        Object action = actionInvocation.getAction();
        System.out.println("action是:======="+action);
        String value;
        //判定Session里的值是否存在
        Map session = ActionContext.getContext().getSession();
        Object uname = session.get("uname");
        String actionName = actionInvocation.getProxy().getActionName();
        actionInvocation.getProxy().getNamespace();//user
        System.out.println("actionName========"+actionName);
        if("login".equals(actionName)){
            System.out.println("actionName是login==============");
            value=actionInvocation.invoke();
        }else if(uname!=null){
            value=actionInvocation.invoke();
            String method = actionInvocation.getProxy().getMethod();
            System.out.println("已经登陆,执行的方法是"+method);
        }else {
            value="login";
        }
        return value;
    }
}
2.写一个实现Action接口的action类

package cn.gh.interceptor.action;

import cn.gh.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

import java.util.Map;

public class UserAction implements Action, ModelDriven {

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String execute() throws Exception {
        Map session = ActionContext.getContext().getSession();
        if (user!=null) {
            if (user.getUsername().equals("1")&&user.getPassword().equals("1")) {
                //省略一个步骤,记录session
                session.put("uname",user.getUsername());
                return SUCCESS;
            }else {
                return LOGIN;
            }
        }else {//不是从页面过来的
            if (session!=null&&session.get("uname")!=null) {
                return "success";
            }else {
                System.out.println("Action中的自定义代码");
                return "login";

            }
        }
    }


    public User getModel() {
        return user;
    }
}
3.写自己的strutsxml文件

xml version="1.0" encoding="UTF-8" ?>
 struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.devMode" value="true">constant>

    <package name="default" namespace="/" extends="struts-default">
        <interceptors>
            <interceptor name="myinter" class="cn.gh.interceptor.myinter.myinterseptor">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">default-interceptor-ref>
        <global-results>
            <result name="login">/login.jspresult>
        global-results>
      
    package>
   
    <include file="struts-06.xml">include>



struts>



xml version="1.0" encoding="UTF-8" ?>
 struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.devMode" value="true">constant>

    <package name="action06" namespace="/" extends="default">


        <action name="success" class="cn.gh.interceptor.action.SuccessAction">
            <result name="success">/success.jspresult>
        action>
        <action name="login" class="cn.gh.interceptor.action.UserAction">
            <result name="success">/success.jspresult>
            <result name="login">/login.jspresult>
        action>

    package>
struts>

你可能感兴趣的:(Struts2 自定义拦截器)