使用Interceptor时,Struts2参数在action类中为null

时间:2012-08-10 12:05:45

标签: struts2 null action interceptor interceptorstack

当我使用Interceptor时,Action类的值变为null。我已经删除了拦截器,这些值在JSP页面中是完美的。

的Login.jsp

    <s:form id="loginFrm" name="loginFrm" action="LoginAction">  
            <s:textfield key="username"/>  
            <s:password key="password"/>  
            <s:submit/>  
    </s:form>  

LoginAction.java

public class LoginAction {  

    private static Logger LOGGER = Logger.getLogger(LoginAction.class);  

    private String username;  
    private String password;  

    public String execute() throws Exception {  
        LOGGER.info("LoginAction : authenticate()");  
        LOGGER.info("LoginAction : {[" + username + "],["+password+ "]}");  
        return "success";  
    }  

    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;  
    }  
}  

的web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
    <display-name>Bulk Fund Switching</display-name>  
    <context-param>  
        <param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </param-name>  
        <param-value>/WEB-INF/tiles.xml</param-value>  
    </context-param>  
    <listener>  
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener </listener-class>  
    </listener>  
    <filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    </filter>  
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    <welcome-file-list>  
        <welcome-file>jsp/Login.jsp</welcome-file>  
    </welcome-file-list>  
</web-app>  

struts.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.enable.DynamicMethodInvocation" value="false" />  
    <constant name="struts.devMode" value="false" />  
    <constant name="struts.custom.i18n.resources" value="BFSView,BFSMessages" />  

    <package name="home-default" extends="struts-default" namespace="/">  

        <result-types>  
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />  
        </result-types>  

        <interceptors>  
            <interceptor name="AuthenticationInterceptor" class="com.lv.bfs.controller.interceptor.AuthenticationInterceptor"></interceptor>  
            <interceptor-stack name="SecureStack">  
                <interceptor-ref name="AuthenticationInterceptor" />  
            </interceptor-stack>  
        </interceptors>  

        <action name="LoginAction" class="com.lv.bfs.controller.action.LoginAction">  
            <result name="success">jsp/Welcome.jsp</result>  
            <result name="error">/jsp/Login.jsp</result>  
        </action>  
    </package>  
</struts>  

struts.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.enable.DynamicMethodInvocation" value="false" />  
    <constant name="struts.devMode" value="false" />  
    <constant name="struts.custom.i18n.resources" value="BFSView,BFSMessages" />  

    <package name="home-default" extends="struts-default" namespace="/">  

        <result-types>  
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />  
        </result-types>  

        <interceptors>  
            <interceptor name="AuthenticationInterceptor" class="com.lv.bfs.controller.interceptor.AuthenticationInterceptor"></interceptor>  
            <interceptor-stack name="SecureStack">  
                <interceptor-ref name="AuthenticationInterceptor" />  
            </interceptor-stack>  
        </interceptors>  

        <action name="LoginAction" class="com.lv.bfs.controller.action.LoginAction">  
            <interceptor-ref name="SecureStack"></interceptor-ref>  
            <result name="success">jsp/Welcome.jsp</result>  
            <result name="error">/jsp/Login.jsp</result>  
        </action>  
    </package>  
</struts>  

AuthenticationInterceptor.java

public class AuthenticationInterceptor extends AbstractInterceptor {  

    private static Logger LOGGER = Logger.getLogger(AuthenticationInterceptor.class);  

    private static final long serialVersionUID = 1844249996954274967L;  

    public String intercept(ActionInvocation invocation) throws Exception {  
        LOGGER.info("intercept : START");  
        return invocation.invoke();  
    }  
}  

记录语句(两种情况)

12:39:05,533 INFO  [AuthenticationInterceptor] intercept : START  
12:39:05,533 INFO  [LoginAction] LoginAction : authenticate()  
12:39:05,533 INFO  [LoginAction] LoginAction : {[null],[null]}  



12:09:05,533 INFO  [LoginAction] LoginAction : authenticate()  
12:09:05,533 INFO  [LoginAction] LoginAction : {[admin],[admin]}  

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

为特定操作定义拦截器堆栈时,必须为该操作定义 all 拦截器。在这种情况下,您定义自定义拦截器,这意味着“params”拦截器不会运行,因此不会设置动作属性。

定义包含普通S2拦截器的新堆栈,或在操作配置中包含那些拦截器(或堆栈)。