title: Passing messages from action to JSP in struts2
body: <p>I am trying to use <code>addActionMessage()</code> and <code>addActionError()</code> to pass messages and error from actions (e.g. in <code>execute()</code>) to the forwarded page. </p>
  
  <p>In the JSP, I use:</p>
  
  <pre><code>&lt;s:if test="hasActionMessages()"&gt;
     &lt;s:actionmessage/&gt;
  &lt;/s:if&gt;
  </code></pre>
  
  <p>to display such messages. </p>
  
  <p>But no message is shown. I am wondering if anyone could give a fix on this problem, or suggest another solution. I am new to Struts and web development, and I am not sure what is a proper pattern for passing messages from actions to pages. </p>
  
  <p>EDIT: action-mapping code and java code</p>
  
  <pre><code>&lt;action name="myAddUser" class="org.any.backend.action.UserAdminAction" method="addUser"&gt;
    &lt;result name="success" type="redirectAction"&gt;myUserAdmin&lt;/result&gt;
    &lt;result name="input" type="redirectAction"&gt;myUserAdmin&lt;/result&gt;
  &lt;/action&gt;
  </code></pre>
  
  <p>Java code: </p>
  
  <pre><code>    public String addUser() throws Exception {
      // check duplicate
      for (User u : userList)
          if (u.getUserName().equals(userName)) {
              addActionError("A user with the same user name already exists. Choose another user name. ");
              return INPUT;
          }
      if (userName != null &amp;&amp; !userName.isEmpty() &amp;&amp; password != null &amp;&amp; !password.isEmpty()) {
          User newUser = new User();
          newUser.setUserName(userName);
          newUser.setPassword(password);
          userList.add(newUser);
          addActionMessage("User " + userName + " added. ");
          return SUCCESS;
      } else {
          addActionError("User name and password cannot be empty");
          return INPUT;
      }
  }
  </code></pre>