struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证

struts2.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>

    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="msg"></constant>
    <constant name="struts.action.extension" value="action,free"></constant>
    <package name="test1" extends="struts-default" abstract="true">
        <interceptors>
            <!--用于显示拦截器的生命周期和调用时间        -->
            <interceptor name="i1" class="com.free.Interceptor.MyInterceptor">
            </interceptor>

            <interceptor name="i2"
                class="com.free.Interceptor.MethodIntercepter">

            </interceptor>

            <interceptor-stack name="my1">
                <!--这里引入struts里面的默认拦截器            -->
                <interceptor-ref name="i2">
                    <!--    login方法不用拦截            -->
                    <param name="excludeMethods">login</param>
                </interceptor-ref>
                <interceptor-ref name="exception" />
                <interceptor-ref name="alias" />
                <interceptor-ref name="servletConfig" />
                <interceptor-ref name="i18n" />
                <interceptor-ref name="prepare" />
                <interceptor-ref name="chain" />
                <interceptor-ref name="scopedModelDriven" />
                <interceptor-ref name="modelDriven" />
                <interceptor-ref name="fileUpload" />
                <interceptor-ref name="checkbox" />
                <interceptor-ref name="multiselect" />
                <interceptor-ref name="staticParams" />
                <interceptor-ref name="actionMappingParams" />
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
                </interceptor-ref>
                <interceptor-ref name="conversionError" />
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
                <interceptor-ref name="workflow">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
                <interceptor-ref name="debugging" />
                <interceptor-ref name="i1">

                </interceptor-ref>

            </interceptor-stack>

        </interceptors>
        <!--配置该包的默认拦截器,且每个包只能指定一个默认拦截器        -->
        <default-interceptor-ref name="my1">

        </default-interceptor-ref>
    </package>

    <package name="test4" extends="test1">
        <action name="delete" class="com.free.Action.loginAction"
            method="delete">
            <result>/success.jsp</result>
            <result name="login">/login1.jsp</result>

        </action>
        <action name="add" class="com.free.Action.loginAction" method="save">
            <result>/success.jsp</result>
            <result name="login">/login1.jsp</result>

        </action>
        <action name="login1" class="com.free.Action.loginAction"
            method="login">
            <result>/admin1.jsp</result>
            <!-- 若出现错误,则转入login1.jsp,就是重新登录        -->
            <result name="login">/login1.jsp</result>
        </action>


    </package>
</struts>

action类

package com.free.Action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class loginAction extends ActionSupport {
    public String save() {
        System.out.println("save");
        return SUCCESS;
    }
    
    public String delete() {
        System.out.println("delete");
        return SUCCESS;
    }
    
    public String login() {
        System.out.println("login方法");
        
        //这里要创立一个标记,建立session
        HttpSession _Session = ServletActionContext.getRequest().getSession();
        _Session.setAttribute("user", "user");
        return SUCCESS;
    }
}

自定义拦截器内容

package com.free.Interceptor;

import java.awt.Desktop.Action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.free.testDemo.action1;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
//import com.opensymphony.xwork2
public class MethodIntercepter extends MethodFilterInterceptor {

    
//    方法拦截器,对于要拦截的方法,放行or不放行
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        HttpSession session = ServletActionContext.getRequest().getSession();
        //判断有误登录标记,有:放行;没有:返回登录的逻辑页面
        Object attribute = session.getAttribute("user");
        if(attribute !=null) {
            System.out.println("检测到了session放行");
            return invocation.invoke();
        } else {
            System.out.println("没有检测到session,转入登录页面");
            return com.opensymphony.xwork2.Action.LOGIN;
            
        }
        
    }

}

 

posted on 2015-09-07 15:37  freedom's_blog  阅读(433)  评论(0编辑  收藏  举报

导航