struts2 Interceptor详解

1.在注册页面中(index.jsp)注册一下:

    <%  
       session.setAttribute("user","lzw");  
    %>  

2.在登录界面(login.jsp)登录:

    <form action="login.action" method="post">  
        <table align="center">  
            <tr>  
                <td>  
                    用户名:  
                td>  
                <td>  
                    <input type="text" name="userName" />  
                td>  
            tr>  
            <tr>  
                <td>  
                    密  码:  
                td>  
                <td>  
                    <input type="password" name="password" />  
                td>  
            tr>  
            <tr align="center">  
                <td colspan="2">  
                    <input type="submit" value="登录" />  
                td>  
            tr>  
        table>  
    form>  

3.登录类:LoginAction.java


    package com.lzw.action;  

    import java.util.Map;  
    import com.opensymphony.xwork2.ActionContext;  
    import com.opensymphony.xwork2.ActionSupport;  

    public class LoginAction extends ActionSupport {  
        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;  
        }  

        @Override  
        public String execute() throws Exception {  
            System.out.println("开始执行execute()");  
            Map map = ActionContext.getContext().getSession();  
            String user = (String) map.get("user");  
            System.out.println("The user'name is ---------- " + user);  
            return SUCCESS;  
        }  
    }  

4.success.jsp页面

    welcome   ${userName} to struts2 world。  

5.拦截器类:LoginInterceper.java


    package com.lzw.intercepter;  

    import java.util.Map;  
    import com.opensymphony.xwork2.Action;  
    import com.opensymphony.xwork2.ActionInvocation;  
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  

    public class LoginIntercepter extends AbstractInterceptor {  
        @Override  
        public String intercept(ActionInvocation invocation) throws Exception {  
            Map session = invocation.getInvocationContext()  
                    .getSession();  
            String user = (String) session.get("user");  
            System.out.println("11---------------- " + user);  
            if ("".equals(user) || null == user) {  
                System.out.println("22---------------- ");  
                return Action.INPUT;  
            } else {  
                System.out.println(" 33---------------- " + user);  
                return invocation.invoke();  
            }  
        }  
    }  

6.struts2配置文件struts.xml


      
    <struts>  
        <package name="struts" extends="struts-default">  
            <interceptors>  
                <interceptor name="authority"  
                    class="com.lzw.intercepter.LoginIntercepter" />  
                <interceptor-stack name="loginStack">  
                    <interceptor-ref name="authority" />  
                    <interceptor-ref name="defaultStack" />  
                interceptor-stack>  
            interceptors>  
            <default-interceptor-ref name="loginStack" />  

            <action name="login" class="com.lzw.action.LoginAction">  
                <result>/success.jspresult>  
                <result name="input" type="redirect">/login.jspresult>  
            action>  
        package>  
    struts>  

7.web.xml


      
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
        <welcome-file-list>  
            <welcome-file>index.jspwelcome-file>  
        welcome-file-list>  
        <filter>  
            <filter-name>struts2filter-name>  
            <filter-class>  
                org.apache.struts2.dispatcher.FilterDispatcher  
            filter-class>  
        filter>  
        <filter-mapping>  
            <filter-name>struts2filter-name>  
            <url-pattern>/*url-pattern>  
        filter-mapping>  

    web-app>  

8.部署到tomcat服务器上,在地址栏上输入:http://localhsot:8080/test来注册,
再重新输入:http://localhost:8080/test/login.jsp

你可能感兴趣的:(struts1)