we could display errors in Struts by doing actionErrors.add(key, new Actionmessage("string")), addErrors(request, actionErrors); and then outputting it into a JSP page via
我们可以通过执行actionErrors.add(key,new Actionmessage(“string”)),addErrors(request,actionErrors)来显示Struts中的错误;然后通过输出到JSP页面
I'm wondering, how do I output success messages in Struts? How do you normally/conventionally do it?
我想知道,如何在Struts中输出成功消息?你通常/常规如何做到这一点?
2 个解决方案
#1
On Struts 1 you can use ActionMessage instances to represent a message to be displayed on a JSP
在Struts 1上,您可以使用ActionMessage实例来表示要在JSP上显示的消息
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message1");
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message2");
saveMessages(request, messages); // storing messages as request attributes
"message1" and "message2" are keys for you resources property file. Very similar to ActionError handling
“message1”和“message2”是资源属性文件的键。与ActionError处理非常相似
Displaying the messages on JSP is similar to action errors, but you must include the property "message"
在JSP上显示消息类似于操作错误,但您必须包含属性“message”
<logic:messagesPresent message="true">
<html:messages id="message" message="true">
<bean:write name="message"/><br/>
</html:messages>
</logic:messagesPresent>
In this example the messages were stored as attribute requests. If you want to have control over the attribute name you can specify any attribute name
在此示例中,消息存储为属性请求。如果要控制属性名称,可以指定任何属性名称
ActionMessages messages = new ActionMessages();
messages.add("appMessage", new ActionMessage("message1");
saveMessages(request, messages); // storing messages as request attributes
Now the messages are stored under the request attribute "appMessage". Setting a custom attribute name may be useful if you want to use JSTL tags instead of Struts tags on JSP for example
现在消息存储在请求属性“appMessage”下。如果要在JSP上使用JSTL标记而不是Struts标记,则设置自定义属性名称可能很有用
Additionally you may save action messages on session scope.
此外,您可以在会话范围上保存操作消息。
saveMessages(request.getSession(), messages); // storing messages as request attributes
You can use this feature to show sticky messages over user session, such as user full name for example.
您可以使用此功能在用户会话上显示粘性消息,例如用户全名。
#2
If you're using Struts2, you should be able to use addActionMessage instead of addActionError.
如果您使用的是Struts2,则应该能够使用addActionMessage而不是addActionError。
Your post is missing what you were putting in your JSP, but if you add an action message, you can use the <s:actionmessage/>
tag to display what you added.
您的帖子缺少您在JSP中添加的内容,但如果添加操作消息,则可以使用
#1
On Struts 1 you can use ActionMessage instances to represent a message to be displayed on a JSP
在Struts 1上,您可以使用ActionMessage实例来表示要在JSP上显示的消息
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message1");
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message2");
saveMessages(request, messages); // storing messages as request attributes
"message1" and "message2" are keys for you resources property file. Very similar to ActionError handling
“message1”和“message2”是资源属性文件的键。与ActionError处理非常相似
Displaying the messages on JSP is similar to action errors, but you must include the property "message"
在JSP上显示消息类似于操作错误,但您必须包含属性“message”
<logic:messagesPresent message="true">
<html:messages id="message" message="true">
<bean:write name="message"/><br/>
</html:messages>
</logic:messagesPresent>
In this example the messages were stored as attribute requests. If you want to have control over the attribute name you can specify any attribute name
在此示例中,消息存储为属性请求。如果要控制属性名称,可以指定任何属性名称
ActionMessages messages = new ActionMessages();
messages.add("appMessage", new ActionMessage("message1");
saveMessages(request, messages); // storing messages as request attributes
Now the messages are stored under the request attribute "appMessage". Setting a custom attribute name may be useful if you want to use JSTL tags instead of Struts tags on JSP for example
现在消息存储在请求属性“appMessage”下。如果要在JSP上使用JSTL标记而不是Struts标记,则设置自定义属性名称可能很有用
Additionally you may save action messages on session scope.
此外,您可以在会话范围上保存操作消息。
saveMessages(request.getSession(), messages); // storing messages as request attributes
You can use this feature to show sticky messages over user session, such as user full name for example.
您可以使用此功能在用户会话上显示粘性消息,例如用户全名。
#2
If you're using Struts2, you should be able to use addActionMessage instead of addActionError.
如果您使用的是Struts2,则应该能够使用addActionMessage而不是addActionError。
Your post is missing what you were putting in your JSP, but if you add an action message, you can use the <s:actionmessage/>
tag to display what you added.
您的帖子缺少您在JSP中添加的内容,但如果添加操作消息,则可以使用