Struts 2 Validation using Message Store Interceptor

jakarta-ee, jsp, struts-validation, struts2, struts2-interceptors

Solution

Use `redirectAction` result instead of `redirect`. And try using configuration in `struts.xml` file instead of annotations.

<action name="userLogin" class="actions.RegisteredUserAction" method="login">
   <interceptor-ref name="store">
      <param name="operationMode">STORE</param>
   </interceptor-ref>
   <interceptor-ref name="defaultStack" />
   <result type="redirectAction" name="input">loginPage</result>
   <result type="redirectAction" name="success">homePage</result>
</action>

Problem

I have an action where I am trying to log the user in. ``` public class RegisteredUserAction extends ActionSupport implements SessionAware { .. .. public String login() throws Exception { DBLogic dBLogic = new DBLogic(); RegisteredUser user = null; try { user = dBLogic.getRegisteredUser(getUserUsername(), getUserPassword()); } catch (CredentialException e) { addFieldError("userUsername", e.getMessage()); addActionError(e.getMessage()); return INPUT; } if (user != null) { session.put("user", user); return SUCCESS; } return ERROR; } } ``` As you can see, if the username or password is invalid, I throw a CredentialException and then populate that exceptions message in my fieldError. I even tried adding an Action Error. With some advice and searching online, I managed to use the Message Store Interceptor using annotations in the RegisteredUserAction.action and the MainAction.action respectively storing and fetching. ``` import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.InterceptorRefs; @InterceptorRefs({ @InterceptorRef(value = "store", params = {"operationMode", "STORE"}), @InterceptorRef("defaultStack"),}) ``` AND ``` import org.apache.struts2.convention.annotation.InterceptorRefs; @InterceptorRefs({ @InterceptorRef(value = "store", params = {"operationMode", "RETRIEVE"}), @InterceptorRef("defaultStack"),}) ``` This is the struts.xml file contents that are relevant: ``` <package name="pages" extends="struts-default" namespace="/"> <global-results> <result name="displayPage">/WEB-INF/template/page.jsp</result> <result name="input">/WEB-INF/template/page.jsp</result> </global-results> <action name="registrationPage" class="actions.MainAction" method="loadRegistrationPage"></action> <action name="loginPage" class="actions.MainAction" method="loadLoginPage"></action> </package> <package name="operations" extends="struts-default" namespace="/"> <action name="userLogin" class="actions.RegisteredUserAction" method="login"> <result type="redirect" name="input">loginPage.action</result> <result type="redirect" name="success">homePage.action</result> </action> </package> ``` I am using a template based approach wherein the Middle content(in my case the LoginPage.jsp) is included into the mainpage dynamically. My page.jsp (which includes the mainContent): ``` <s:include value="../%{mainContent}.jsp" ></s:include> ``` My login.jsp (which gets included): ``` <%@taglib prefix="s" uri="/struts-tags"%> <s:form action="userLogin" method="POST"> <s:textfield name="userUsername" />Error:<s:fielderror name="userUsername" /> <s:password name="userPassword" /> <s:submit /> </s:form> ``` The redirect works properly and the login form appears, but no errors. I've even tried using the Message Store interceptor in the struts.xml with no luck. :(

Original source