我通过以下命令通过 JSP
文件显示 ActionMessage
:
<logic:messagesPresent message="true">
<ul id="messsages">
<html:messages id="msg" message="true">
<li><bean:write name="msg"/> </li>
</html:messages>
</ul>
</logic:messagesPresent>
现在我只想显示选定的消息。如何指示要显示的消息?
已更新
实际上我有两个 ActionMessages
对象 - messages
和 warnings
。
现在我想在单独的 JSP
页面上显示它们...一个页面显示 messages
另一个页面显示 warnings
。
那么如何在JSP
页面中指明显示哪些消息呢?
已更新 - 2
现在,我发现了一件奇怪的事情。
saveMessages(request, messages);
saveMessages(request, warnings);
当我写上面的代码时,只有 warnings
起作用了。当我颠倒上面两个语句的顺序时,只有 messages
起作用。
似乎我们只能在请求中添加一个ActionMessages
对象。如果正确,那么如何在两个ActionMessages
对象中分别显示消息。
最佳答案
简单,
分开你的messages
和你的 warnings
: 在你的 struts Action 中,保存你的消息和警告如下:
//For messages
saveMessages(request, messages);
//For warnings
saveErrors(request, warnings);
显示它们:
<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>
这会显示所有 messages
(通过设置 message="true"
)
<html:messages id="aMsg" message="true">
这会显示所有 warnings
(通过设置 message="false"
)
<html:messages id="aMsg" message="false">
更新看到您现在正在清除您的问题,最简单的方法就是这样做。
有一个特定的标志,表明用户是否想查看messages
或 warnings
.在 Struts Action 上,请求标志并检查用户是否选择了查看消息或警告。
然后您保存 warnings
或 messages
根据用户选择并显示相同的页面(如您上面所写)以显示消息。
原因是这样的,Struts(在存储消息或错误时)使用以下常量根据请求或 session 存储它。
- Globals.MESSAGE_KEY(当您执行
saveMessages(request, messages)
时分配) - Globals.ERROR_KEY(当您执行
saveErrors(request, errors)
时分配)
当使用 <logic:messagesPresent message="true">
时, Struts 搜索 MESSAGE_KEY
(如果消息=真)或ERROR_KEY
(如果 message=false)或两者(如果 message=none)。你无法控制它。
<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.
您还可以编写小脚本来检查这些键是否存在,然后 <logic:iterate />
通过键来显示消息(但那将是太多的工作)。
希望这对您有所帮助。
关于java - Struts 1 - 如何显示 ActionMessages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2403194/