Passing messages from action to JSP in struts2
Solution 1
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
<s:if test="hasActionErrors()">
<s:actionerror />
</s:if>
<s:if test="hasActionMessages()">
<s:actionmessage/>
</s:if>
and not only the posted one, or you will never see the errors, only the messages...
Solution 2
As I have seen you are using redirect action, you can still keep the action error/messages using one of the interceptors.
http://struts.apache.org/2.3.1.2/docs/message-store-interceptor.html
Here's an example in the struts.xml
<action name="submit"
class="com.dummy.SubmitAction">
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success" type="redirectAction">
<param name="actionName">view</param>
<param name="asnid">${id}</param>
</result>
</action>
<action name="view"
class="com.dummy.ViewUserAction">
<interceptor-ref name="store">
<param name="operationMode">RETRIEVE</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" >
<param name="workflow.excludeMethods">execute</param>
</interceptor-ref>
<result name="success">pages/user.jsp</result>
</action>
Solution 3
Use session.setAttribute(...)
in Action Class. and <logic:present .... >
tag in jsp.
hope this will help you.
qinsoon
Updated on June 04, 2022Comments
-
qinsoon almost 2 years
I am trying to use
addActionMessage()
andaddActionError()
to pass messages and error from actions (e.g. inexecute()
) 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; } }
-
qinsoon over 11 yearsYes! I am using redirect-action. That explains it! Thank you very much.