3

当我使用拦截器时,Action 类的值为 null。我已经删除了拦截器,这些值来自 JSP 页面。

登录.jsp

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

登录操作.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]}  

有什么建议么?

4

1 回答 1

4

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

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

于 2012-08-10T13:19:54.577 回答