title: Not able to view ActionMessages in my JSP and problems with validation
body: <p>I am having various problems with my code:</p>
  <ol>
  <li>I am not able to view my <code>ActionMessages</code> and <code>ActionErrors</code> set by <code>LoginAction</code></li>
  <li>Once I log in, if I again go to URL: <code>/login</code>, it does not redirect me to success page.</li>
  </ol>
  <p><strong>EDIT:</strong></p>
  <ol>
  <li>I am able to redirect successfully now if the session alreay exists. I had to make 2 changes that i figured out:</li>
  </ol>
  <ul>
  <li>Firstly, in my action class I was setting session variables in <code>void setSession(Map map)</code> which was wrong( It should only be a setter function).</li>
  <li>Secondly, I checked if the session variable exists in my <code>validate()</code> method which solved the problem.</li>
  </ul>
  <ol start="2">
  <li><p>But I am still not able to view my <code>ActonErrors</code> and <code>ActionMessages</code>.</p>
  </li>
  <li><p>Is it necessary to use <code>&lt;s:form&gt;</code>. Because it disturbs my CSS and I am not able to find solutions to correct that. Can I assign ID attribute in <code>&lt;s:form&gt;</code> ?</p>
  </li>
  </ol>
  <p>Following is my JSP code:</p>
  <pre class="lang-jsp prettyprint-override"><code>...
          &lt;s:if test=&quot;hasActionErrors()&quot;&gt;
              &lt;div id=&quot;errorMessage&quot;&gt;&lt;s:actionerror /&gt;&lt;/div&gt;
          &lt;/s:if&gt;
          &lt;s:if test=&quot;hasActionMessages()&quot;&gt;
              &lt;div id=&quot;errorMessage&quot;&gt; &lt;s:actionmessage /&gt;&lt;/div&gt;
          &lt;/s:if&gt;
          &lt;form action=&quot;login&quot; method=&quot;POST&quot; id=&quot;login-form&quot;&gt;
              &lt;fieldset&gt;
                  &lt;p&gt;
                      &lt;label for=&quot;loginUserName&quot;&gt;username&lt;/label&gt;
                      &lt;input type=&quot;text&quot; id=&quot;loginUserName&quot; maxlength=&quot;10&quot; placeholder=&quot;Eg. EMP0000000&quot; 
                      title=&quot;User ID is of format EMP0000000&quot; pattern=&quot;(EMP)([0-9]{7})&quot; required
                       name=&quot;loginUserName&quot; class=&quot;round full-width-input&quot; autofocus /&gt;
                  &lt;/p&gt;
  
                  &lt;p&gt;
                      &lt;label for=&quot;loginPassword&quot;&gt;password&lt;/label&gt;
                      &lt;input type=&quot;password&quot; name=&quot;loginPassword&quot; maxlength=&quot;20&quot; id=&quot;loginPassword&quot; required 
                      class=&quot;round full-width-input&quot; /&gt;
                  &lt;/p&gt;
                  
                  &lt;p&gt;I've &lt;a href=&quot;#&quot;&gt;forgotten my password&lt;/a&gt;.&lt;/p&gt;
                  
                  &lt;input type=&quot;submit&quot; class=&quot;button round blue image-right ic-right-arrow&quot; value=&quot;LOG IN&quot; /&gt;
  
              &lt;/fieldset&gt;
  
          &lt;/form&gt;
  ...
  </code></pre>
  <p><strong>Here is my <code>struts.xml</code>:</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
  &lt;!DOCTYPE struts PUBLIC
  &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quot;
  &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;
          
  &lt;struts&gt;
          &lt;constant name=&quot;struts.enable.DynamicMethodInvocation&quot; value=&quot;false&quot; /&gt;
          &lt;constant name=&quot;struts.devMode&quot; value=&quot;false&quot; /&gt;
  
          &lt;package name=&quot;employee&quot; extends=&quot;struts-default&quot; namespace=&quot;/&quot;&gt;
  
              &lt;interceptors&gt;
                  &lt;interceptor name=&quot;login&quot; class=&quot;com.sreebhog.interceptors.LoginInterceptor&quot; /&gt;
          
                  &lt;interceptor-stack name=&quot;loginStack&quot;&gt;
                      &lt;interceptor-ref name=&quot;login&quot; &gt;&lt;/interceptor-ref&gt;
                      &lt;interceptor-ref name=&quot;defaultStack&quot; &gt;&lt;/interceptor-ref&gt;
                  &lt;/interceptor-stack&gt;
              &lt;/interceptors&gt;
                  &lt;default-interceptor-ref name=&quot;loginStack&quot;&gt;&lt;/default-interceptor-ref&gt;
              &lt;global-results&gt;
                  &lt;result name=&quot;loginRedirect&quot;&gt;/login &lt;/result&gt;
              &lt;/global-results&gt;
          
              &lt;action name=&quot;login&quot; class=&quot;com.sreebhog.actions.LoginAction&quot;&gt;
                  &lt;result name=&quot;input&quot;&gt;/JSP/sreebhog_login.jsp&lt;/result&gt;
                  &lt;result name=&quot;success&quot;&gt;/JSP/index.jsp&lt;/result&gt;
              &lt;/action&gt;
              &lt;action name=&quot;logout&quot; class=&quot;com.sreebhog.actions.LogoutAction&quot;&gt;
                  &lt;result name=&quot;logout&quot;&gt;/JSP/sreebhog_login.jsp&lt;/result&gt;
              &lt;/action&gt;
          &lt;/package&gt;
  
  
  &lt;/struts&gt;
  </code></pre>
  <p><strong>Here is my <code>LoginAction</code>:</strong></p>
  <pre><code>package com.sreebhog.actions;
  
  import java.io.UnsupportedEncodingException;
  import java.security.NoSuchAlgorithmException;
  import java.util.Map;
  
  import org.apache.struts2.dispatcher.SessionMap;
  import org.apache.struts2.interceptor.SessionAware;
  
  import com.opensymphony.xwork2.ActionSupport;
  import com.sreebhog.dao.EmployeeDAO;
  import com.sreebhog.interfaces.LoginRequired;
  import com.sreebhog.model.dto.Employee;
  import com.sreebhog.utility.Cryptography;
  
  public class LoginAction extends ActionSupport implements SessionAware,LoginRequired {
      
      private String loginUserName;
      private String loginPassword;
      
      SessionMap&lt;String,Object&gt; sessionMap;
      
      @SuppressWarnings({ &quot;unchecked&quot;, &quot;rawtypes&quot; })
      @Override
      public void setSession(Map map) {
          sessionMap=(SessionMap) map;
          sessionMap.put(&quot;username&quot;, loginUserName);
      }
          
      public String execute()
      {
          String userName = null;
            if (sessionMap.containsKey(&quot;username&quot;))
            { 
                userName = (String) sessionMap.get(&quot;username&quot;);
                if (userName != null )
                {
                    if (!userName.equals(&quot;&quot;))
                    {
                        return SUCCESS;
                    }
                }
            }
            
          EmployeeDAO employeeDao = new EmployeeDAO();
          Employee employee = employeeDao.find(this.getLoginUserName());
          if (employee == null || employee.getUserName().equals(&quot;&quot;))
          {
              System.out.println(&quot;Here1&quot;);
              addActionError(&quot;Username or password is invalid&quot;);
              return INPUT;
          } else
              try {
                  if (employee.getPassword().equals(Cryptography.getHash(&quot;SHA-1&quot;, this.getLoginPassword(), employee.getSalt())))
                  {
                      System.out.println(&quot;Here2&quot;);
                      addActionError(&quot;Username or password is invalid&quot;);
                      return INPUT;
                  }
                  else {
                      System.out.println(&quot;Here3&quot;);
                      sessionMap.put(&quot;username&quot;,(String)loginUserName);
                      sessionMap.put(&quot;role&quot;,(String)employee.getPermissions().getRole());
                      return SUCCESS;
                  }
              } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
                  e.printStackTrace();
              }
          return INPUT;
      }
      
      public void validate()
      {
          
          if (loginUserName == null || loginUserName.equals(&quot;&quot;) || loginPassword == null || loginPassword.equals(&quot;&quot;))
          {
              addActionError(&quot;Please fill in username and password&quot;);
          }
      }
  
      public String logout()
      {
          sessionMap.invalidate();
          return INPUT;
      }
  
      public String getLoginUserName() {
          return loginUserName;
      }
  
      public void setLoginUserName(String loginUserName) {
          this.loginUserName = loginUserName;
      }
  
      public String getLoginPassword() {
          return loginPassword;
      }
  
      public void setLoginPassword(String loginpassword) {
          this.loginPassword = loginpassword;
      }
  
  }
  </code></pre>
  <p><strong>Here is my <code>LoginInterceptor</code>:</strong></p>
  <pre><code>package com.sreebhog.interceptors;
  
  import java.util.Map;
  
  import com.opensymphony.xwork2.ActionContext;
  import com.opensymphony.xwork2.ActionInvocation;
  import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  import com.sreebhog.actions.LoginAction;
  import com.sreebhog.interfaces.LoginRequired;
  
  public class LoginInterceptor extends AbstractInterceptor {
  
      @Override
      public String intercept(final ActionInvocation invocation) throws Exception {
          Map&lt;String, Object&gt; session = ActionContext.getContext().getSession();
          String userName = (String) session.get(&quot;username&quot;);
  
          if (userName != null &amp;&amp; !userName.equals(&quot;&quot;)) {
              return invocation.invoke();
          }
  
          Object action = invocation.getAction();
  
          if (!(action instanceof LoginRequired)) {
              return invocation.invoke();
          }
  
          if (!(action instanceof LoginAction)) {
              return &quot;loginRedirect&quot;;
          }
  
          return invocation.invoke();
      }
  }
  </code></pre>