I am trying to use addActionMessage() and addActionError() to pass messages and error from actions (e.g. in execute()) to the forwarded page.
In the JSP, I use:
<s:if test="hasActionMessages()">
<s:actionmessage/>
</s:if>
to display such messages.
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.
EDIT: action-mapping code and java code
<action name="myAddUser" class="org.any.backend.action.UserAdminAction" method="addUser">
<result name="success" type="redirectAction">myUserAdmin</result>
<result name="input" type="redirectAction">myUserAdmin</result>
</action>
Java 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 && !userName.isEmpty() && password != null && !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;
}
}
Your code is right.
Maybe you are using a REDIRECT-ACTION result type, or a CHAIN.
They both will lose action messages and errors, forcing you to put them in session (and clean them once displayed) for this page.
EDIT: I’m assuming that you are using the block
and not only the posted one, or you will never see the errors, only the messages…