Java 如何在 Struts 中显示成功消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/709905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display success message in Struts?
提问by ajushi
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 中显示错误。然后通过
I'm wondering, how do I output success messages in Struts? How do you normally/conventionally do it?
我想知道,如何在 Struts 中输出成功消息?你通常/传统上是怎么做的?
回答by dbrown0708
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 中放置的内容,但是如果您添加操作消息,您可以使用该<s:actionmessage/>
标签来显示您添加的内容。
回答by Daniel Melo
On Struts 1 you can use ActionMessageinstances 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 上显示消息类似于操作错误,但您必须包含属性“消息”
<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.
您可以使用此功能在用户会话中显示粘性消息,例如用户全名。