title: Struts 2 Validation using Message Store Interceptor
body: <p>I have an action where I am trying to log the user in.</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>import org.apache.struts2.convention.annotation.InterceptorRef;
  import org.apache.struts2.convention.annotation.InterceptorRefs;
  @InterceptorRefs({
      @InterceptorRef(value = "store", params = {"operationMode", "STORE"}),
      @InterceptorRef("defaultStack"),})
  </code></pre>
  
  <p>AND</p>
  
  <pre><code>import org.apache.struts2.convention.annotation.InterceptorRefs;
  
  @InterceptorRefs({
      @InterceptorRef(value = "store", params = {"operationMode", "RETRIEVE"}),
      @InterceptorRef("defaultStack"),})
  </code></pre>
  
  <p>This is the struts.xml file contents that are relevant:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;package name="pages" extends="struts-default" namespace="/"&gt;
          &lt;global-results&gt;
              &lt;result name="displayPage"&gt;/WEB-INF/template/page.jsp&lt;/result&gt;
              &lt;result name="input"&gt;/WEB-INF/template/page.jsp&lt;/result&gt;
          &lt;/global-results&gt;
          &lt;action name="registrationPage" class="actions.MainAction" 
                method="loadRegistrationPage"&gt;&lt;/action&gt;
          &lt;action name="loginPage" class="actions.MainAction" 
                method="loadLoginPage"&gt;&lt;/action&gt;
  &lt;/package&gt;
  
  &lt;package name="operations" extends="struts-default" namespace="/"&gt;
  
          &lt;action  name="userLogin" class="actions.RegisteredUserAction" method="login"&gt;
              &lt;result type="redirect" name="input"&gt;loginPage.action&lt;/result&gt;
              &lt;result type="redirect" name="success"&gt;homePage.action&lt;/result&gt;
          &lt;/action&gt;
  &lt;/package&gt;
  </code></pre>
  
  <p>I am using a template based approach wherein the Middle content(in my case the LoginPage.jsp) is included into the mainpage dynamically.</p>
  
  <p>My page.jsp (which includes the mainContent):</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;s:include value="../%{mainContent}.jsp" &gt;&lt;/s:include&gt;
  </code></pre>
  
  <p>My login.jsp (which gets included):</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;%@taglib prefix="s" uri="/struts-tags"%&gt;
  
  &lt;s:form action="userLogin" method="POST"&gt;
      &lt;s:textfield name="userUsername" /&gt;Error:&lt;s:fielderror name="userUsername" /&gt;
      &lt;s:password name="userPassword" /&gt;
      &lt;s:submit /&gt;
  &lt;/s:form&gt;
  </code></pre>
  
  <p>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. :(</p>