How to print a Struts2 ActionMessage in a JavaScript alert box(如何在 JavaScript 警告框中打印 Struts2 ActionMessage)
问题描述
我有一个 Action 类,它使用 addActionMessage(); 发送动作消息.
I have one Action class which is sending an action message using addActionMessage();.
我想使用 JavaScript 警告框在我的 JSP 中显示该消息.
I want to display that message in my JSP using a JavaScript alert box.
我试过了
alert ('<s:actionmessage/>');
但它显示的是未终止的字符串文字.
推荐答案
<s:actionmessage/> 标签 生成 HTML,如下所示:
<ul class="actionMessage">
<li>
<span>
Your Message 1
</span>
</li>
<li>
<span>
Your Message 2, and so on...
</span>
</li>
</ul>
这将使用无效的 JS 代码破坏您的 javascript 函数:
This will break your javascript function with invalid JS code:
alert('<ul class="actionMessage">
<li>
...
');
解决方案是手动迭代您的消息,并自己构建输出,使用 <s:property/>,而不是使用 <s:actionmessage/>:
The solution is to manually iterate over your messages, and build the output by yourself, with <s:property/>, instead of using <s:actionmessage/>:
<s:if test="actionMessages!=null && actionMessages.size > 0">
<script>
var actionMessages;
<s:iterator value="actionMessages" >
// Iterate the messages, and build the JS String
actionMessages += '-' + '<s:property />' + '
';
</s:iterator>
alert (actionMessages);
</script>
</s:if>
还记得将 actionMessages 中的单引号括起来.
Also remember to wrap the single quotes from your actionMessages.
这篇关于如何在 JavaScript 警告框中打印 Struts2 ActionMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 JavaScript 警告框中打印 Struts2 ActionMessage
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
