拦截器

   pom.xml   

<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.3version>
    <scope>testscope>
dependency>


<dependency>
    <groupId>javaeegroupId>
    <artifactId>javaee-apiartifactId>
    <version>5version>
dependency>

<dependency>
    <groupId>org.apache.strutsgroupId>
    <artifactId>struts2-coreartifactId>
    <version>2.3.4.1version>
dependency>

<dependency>
    <groupId>org.apache.struts.xworkgroupId>
    <artifactId>xwork-coreartifactId>
    <version>2.3.4.1version>
dependency>

         web.xml

<filter>
  <filter-name>struts2filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
  <filter-name>struts2filter-name>
  <url-pattern>/*url-pattern>
filter-mapping>

  action
BookAction类
public class BookAction implements Action {
    public String execute() throws Exception {
        System.out.println("bookaction-----------------");
        return SUCCESS;
    }
}
LoginAction类
public class LoginAction implements Action {
    private UserInfo userInfo;
    public String execute() throws Exception {
        Map session= ActionContext.getContext().getSession();
        if(userInfo!=null){
            if(userInfo.getUsername().equals("1")&&userInfo.getPassword().equals("1")){
                //记录session
                session.put("uname",userInfo.getUsername());
                return SUCCESS;
            }else {
                return LOGIN;
            }
        }else {
            //不是从页面过来的
            if(session!=null&&session.get("uname")!=null){
                return "success";
            }else {
                System.out.println("Action 中的自定义代码");
                return "login";
            }
        }
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }
}
entity
UserInfo类
public class UserInfo {
    private String username;
    private String password;

    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;
    }
}
Interceptor
MyInterceptor类
public class MyInterceptor 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里面key值对应的value是否存在
        Map session= ActionContext.getContext().getSession();
        Object name=session.get("uname");
        String actionnmae=actionInvocation.getProxy().getActionName();
        actionInvocation.getProxy().getNamespace();  //user
        System.out.println("actionname--------"+actionnmae);
        if(actionnmae.equals("login")){
            System.out.println("*********************action是login");
            //user/login.action
            value=actionInvocation.invoke();
        }else if(name!=null){
            //已经登陆,放行
            value=actionInvocation.invoke();
            String  method=actionInvocation.getProxy().getMethod();
            System.out.println("方法:==="+value);
        }else {
            value="login";
        }
        System.out.println("逻辑视图名"+value);
        return value;
    }
}
struts-day06.xml
<struts>
    <constant name="struts.devMode" value="true">constant>
    <package name="day06" namespace="/" extends="default">
        <action name="login" class="cn.struts.day06.action.LoginAction">
            <result name="success">/WEB-INF/myinter/success.jspresult>
            <result name="login">/WEB-INF/myinter/login.jspresult>
        action>

        <action name="book" class="cn.struts.day06.action.BookAction">
            <result name="success">/WEB-INF/myinter/booksuccess.jspresult>
        action>
        package>
        struts>
struts.xml
 
<struts>
<constant name="struts.devMode" value="true">constant>
    <package name="default" namespace="/" extends="struts-default">
        <interceptors>
            <interceptor name="myInter" class="cn.struts.day06.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">default-interceptor-ref>

        <default-action-ref name="first">default-action-ref>

        <action name="first">action>

    package>



<include file="struts-day06.xml">include>
struts>
login.jsp
<body>
<h2>登陆234h2>
<s:form method="POST" action="login">
    请输入用户名<s:textfield name="userInfo.username">s:textfield>
<s:textfield name="userInfo.password">s:textfield><br/>
    <s:submit value="登陆">s:submit>
s:form>
body>
success.jsp
<head>
    <title>Titletitle>
head>
<body>
<h2>success!!!!h2>
body>
拦截器_第1张图片

你可能感兴趣的:(拦截器)