2 u011339565 u011339565 于 2013.12.03 14:04 提问

strus2继承MethodFilterInterceptor创建的拦截器不起作用

Action 代码如下:

package com.silveraegis.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;

//登录login
public String login(){
    System.out.println("testLogin");
    return "index";
}


public String TestMethod(){
    System.out.println("TestMethod");
    return "index";
}


public String TestMethod2(){
    System.out.println("TestMethod2");
    return "index";
}
public String TestMethod3(){
    System.out.println("TestMethod3");
    return "index";
}

}

拦截器代码如下:

package com.silveraegis.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class TestMethodInterceptor extends MethodFilterInterceptor{

/**
 * 
 */
private static final long serialVersionUID = 1L;

protected String doIntercept(ActionInvocation invocation) throws Exception {
    //只要是你配置的需要拦截的方法 那么他就会进入这个拦截器    而在拦截器内 处理你自己的业务逻辑

    System.out.println("此方法会被过滤:"+invocation.getProxy().getMethod());

    return invocation.invoke();
}

}

配置文件代码如下:

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





testLogin
TestMethod,TestMethod2,TestMethod3




    </interceptors>
    <!--<default-interceptor-ref name="testMethod"></default-interceptor-ref>
    --><action name="LoginAction" class="com.silveraegis.action.LoginAction" method="testLogin,TestMethod,TestMethod2,TestMethod3">
        <interceptor-ref name="testMethod">

        </interceptor-ref>

        <result name="index">/index.jsp</result>
    </action>
</package>

jsp页面如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


           <form action="test/LoginAction!login" >

                <input type="submit" value="TestLogin">

           </form>
           <form action="test/LoginAction!TestMethod" >

                <input type="submit" value="TestMethod">

           </form>
           <form action="test/LoginAction!TestMethod2" >

                <input type="submit" value="TestMethod2">

           </form>
           <form action="test/LoginAction!TestMethod3" >

                <input type="submit" value="TestMethod3">

           </form>






        </div>


Csdn user default icon
上传中...
上传图片
插入图片