Java Struts 1 - 如何显示 ActionMessages
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2403194/
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
Struts 1 - How to display ActionMessages
提问by Yatendra Goel
I am displaying ActionMessage
s through a JSP
file by the following command:
我ActionMessage
通过JSP
以下命令通过文件显示s :
<logic:messagesPresent message="true">
<ul id="messsages">
<html:messages id="msg" message="true">
<li><bean:write name="msg"/> </li>
</html:messages>
</ul>
</logic:messagesPresent>
Now I want to display only selected messages. How can I indicate which message to display?
现在我只想显示选定的消息。如何指示要显示的消息?
Updated
更新
Actually I have two ActionMessages
object - messages
and warnings
.
Now I want to display both of them on separate JSP
page... One page to show messages
and other for warnings
.
实际上我有两个ActionMessages
对象 -messages
和warnings
. 现在我想在单独的JSP
页面上显示它们......一页显示messages
,另一页显示warnings
.
So how to indicate in the JSP
page that which messages to display?
那么如何在JSP
页面中指明要显示哪些消息呢?
Updated - 2
更新 - 2
Now, I found a strange thing.
现在,我发现了一件奇怪的事情。
saveMessages(request, messages);
saveMessages(request, warnings);
When I wrote the above code, only warnings
was working. When I reversed the order of the above two statements, then only messages
was working.
当我写上面的代码时,只有warnings
在工作。当我颠倒上述两个语句的顺序时,然后才messages
起作用。
It seems that we can add only one ActionMessages
object in a request. If it is correct, then how to display messages in two ActionMessages
objects seperately.
似乎我们只能ActionMessages
在一个请求中添加一个对象。如果正确,那么如何在两个ActionMessages
对象中分别显示消息。
采纳答案by Buhake Sindi
Simple,
简单的,
Separate your messages
and your warnings
: In your struts action, save your messages and warnings as follows:
将您的messages
和您的warnings
: 在您的 struts 操作中,按如下方式保存您的消息和警告:
//For messages
saveMessages(request, messages);
//For warnings
saveErrors(request, warnings);
To display them:
要显示它们:
<logic:messagesPresent message="true">
<html:messages id="aMsg" message="true">
<logic:present name="aMsg">
<!-- Messages -->
<div class="messages">
<bean:write name="aMsg" filter="false" />
</div>
</logic:present>
</html:messages>
</logic:messagesPresent>
<logic:messagesPresent message="false">
<html:messages id="aMsg" message="false">
<logic:present name="aMsg">
<!-- Warnings-->
<div class="warnings">
<bean:write name="aMsg" filter="false" />
</div>
</logic:present>
</html:messages>
</logic:messagesPresent>
This displays all messages
(by setting message="true"
)
这将显示所有messages
(通过设置message="true"
)
<html:messages id="aMsg" message="true">
This displays all warnings
(by setting message="false"
)
这将显示所有warnings
(通过设置message="false"
)
<html:messages id="aMsg" message="false">
UPDATESeeing that you're now clearing your question, the simplest way would be to do this.
更新看到您现在正在清除问题,最简单的方法就是这样做。
Have a certain flag that will indicate whether the user would like to view messages
or warnings
. On the Struts Action, request the flag and check if the user selected viewing messages or warnings.
You then save either warnings
or messages
based on the user selection and display the same page (as you wrote above) to display messages.
有一个特定的标志,将指示用户是否想要查看messages
或warnings
。在 Struts 操作上,请求标志并检查用户是否选择查看消息或警告。然后您保存warnings
或messages
基于用户选择并显示相同的页面(如您上面所写)以显示消息。
The reason is this, Struts (when storing you messages or errors) stores it on request or session with the following constant.
原因是,Struts(在存储消息或错误时)根据请求或会话使用以下常量存储它。
- Globals.MESSAGE_KEY (that is assigned when you do
saveMessages(request, messages)
) - Globals.ERROR_KEY (that is assigned when you do
saveErrors(request, errors)
)
- Globals.MESSAGE_KEY(当你这样做时分配
saveMessages(request, messages)
) - Globals.ERROR_KEY(当你这样做时分配
saveErrors(request, errors)
)
when using <logic:messagesPresent message="true">
, Struts searches for the MESSAGE_KEY
(if message=true) or ERROR_KEY
(if message=false) or both (if message=none). You have no control of that.
使用 时<logic:messagesPresent message="true">
,Struts 搜索MESSAGE_KEY
(if message=true) 或ERROR_KEY
(if message=false) 或两者(如果 message=none)。你无法控制。
<html:messages />
TLD comments states:
<html:messages />
TLD 评论指出:
By default the tag will retrieve the bean it will iterate over from the Globals.ERROR_KEY constant string,
but if this attribute is set to 'true' the bean will be retrieved from the Globals.MESSAGE_KEY constant string. Also if this is set to 'true', any value assigned to the name attribute will be ignored.
默认情况下,标签将检索将从 Globals.ERROR_KEY 常量字符串迭代的 bean,
但如果此属性设置为 'true',将从 Globals.MESSAGE_KEY 常量字符串检索 bean。此外,如果将其设置为“true”,则分配给 name 属性的任何值都将被忽略。
You can also write scriptlet to check if those keys exists, then <logic:iterate />
through the key to display the messages (but that'll be too much work).
您还可以编写 scriptlet 来检查这些键是否存在,然后<logic:iterate />
通过键来显示消息(但是那样工作量太大)。
Hope this helps.
希望这可以帮助。
回答by highlycaffeinated
Instead of using the message
attribute on the messagesPresent
and messages
tags, you can use the name
attribute to specify the name of the ActionMessages object you have in page, request, session, or application scope.
您可以使用该属性来指定页面、请求、会话或应用程序范围中的 ActionMessages 对象的名称,而不是message
在messagesPresent
和messages
标签上使用该name
属性。