问题描述
我有一个使用 addActionMessage(); 发送动作消息的 Action 类。
我想使用JavaScript警告框在我的JSP中显示该消息。
我试过了
alert('< s :ActionMessage的/>');
但它显示未终止的字符串字面值。 如下所示:
< ul class =actionMessage>
< li>
< span>
您的留言1
< / span>
< / li>
< li>
< span>
您的留言2,等等...
< / span>
< / li>
< / ul>
这会让你的javascript函数无效的JS代码破坏:
alert('< ul class =actionMessage>
< li>
...
');
解决方案是手动迭代您的消息,并自己构建输出,使用< s:property /> ,而不是使用< s:actionmessage /> :
< s:if test =actionMessages!= null&& actionEvents.size> 0>
< script>
var actionMessages;
< s:iterator value =actionMessages>
//迭代消息,并构建JS String
actionMessages + =' - '+'< s:property />'+'\\\
';
< / s:iterator>
alert(actionMessages);
< / script>
< / s:if>
还要记住从actionMessages中包装单引号。
I have one Action class which is sending an action message using addActionMessage();.
I want to display that message in my JSP using a JavaScript alert box.
I've tried with
alert ('<s:actionmessage/>');
but it is showing unterminated string literal.
The <s:actionmessage/> tag generates an HTML like the following:
<ul class="actionMessage"> <li> <span> Your Message 1 </span> </li> <li> <span> Your Message 2, and so on... </span> </li> </ul>
This will break your javascript function with invalid JS code:
alert('<ul class="actionMessage"> <li> ... ');
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 />' + '\n'; </s:iterator> alert (actionMessages); </script> </s:if>
Also remember to wrap the single quotes from your actionMessages.
这篇关于如何在JavaScript警告框中打印Struts2 ActionMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!