Struts 2 action variables not populated after inte

2019-03-29 21:03发布

问题:

My Problem is that the action's variables are not being populated after it is triggered from the interceptor using invocation.invoke. What i know is that before using an interceptor , it worked properly (From a .jsp i submitted a form and an action is called and every variable was populated)

Interceptor class:

public String intercept(ActionInvocation invocation) throws Exception {

    final ActionContext context = invocation.getInvocationContext();
    HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
    HttpSession session = request.getSession(true);
    Object user = session.getAttribute(Constants.USER_HANDLE);

    if (user == null) {
        String signUp = request.getParameter(Constants.SIGN_UP);

        if (!StringUtils.isBlank(loginAttempt)) {
            if (processLoginAttempt(request, session)) {
                return "login-success";
            }
        } else if (!StringUtils.isBlank(signUp)) {
            return invocation.invoke();
        }
        return "login";
    } else {
        return invocation.invoke();
    }
}

Struts XML file :

<package name="defaultPackage" extends="struts-default">

    <interceptors>
        <interceptor name="login" class="com.spiddan.LoginInterceptor" />
        <interceptor-stack name="defaultStack">
            <interceptor-ref name="login"/>
        </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="defaultStack"/>
    <default-action-ref name="index"/>

    <global-results>
        <result name="login" type="redirect">/logIn.jsp</result>
        <result name="login-success" type="redirect">index</result>
    </global-results>

    <action name="addUserAction" class="com.spiddan.action.UserAction" method="addUser">
        <result name="input">/logIn.jsp</result>
        <result name="success">/index.jsp</result>
        <result name="error">/error.jsp</result>
    </action>

Action class :

public class UserAction extends ActionSupport implements ModelDriven {

private User user;
private int tempGender;
private String confirmPassword;
private UserDAO userDAO;
private PasswordEncrypter passwordEncrypter;

public Object getModel() {
    return getUser();
}

public String addUser() throws Exception {

}

 //Getter and setters ect 

回答1:

You've specified only your custom interceptor in your code and have excluded all the other Struts2 default interceptors. Try this instead:

<interceptors>
    <interceptor name="login" class="com.spiddan.LoginInterceptor" />
    <interceptor-stack name="defaultStackModified">  <!-- name doesn't override existing defaultStack-->
        <interceptor-ref name="defaultStack"/>       <!-- include the defaultStack this way--> 
        <interceptor-ref name="login"/>
    </interceptor-stack>
</interceptors>