Formatting Issue in HTML messages from ResourceBundle using <s:actionerror/> or <s:actionmessage/> in Struts2

让人想犯罪 __ 提交于 2019-12-02 09:49:43

The <s:actionerror/> and <s:actionmessage/> tags will encapsulate your actionerrors and actionmessages in <ul><li><span>yourmessage</span></li></ul>.

This will make your message starting with a circle dot (because it is inside an <li>) and the other nested <li> with the circle outline (as defined by HTML for second-level <li>).

This:

<s:actionmessage />

will generate:

<ul class="actionMessage"><li><span>

    <font color="red">The password you have entered does not 
    meet password strength requirements.  Please select a new password that conforms 
    to the following standards:<UL><LI>Minimum length of 8 characters</LI>
    <LI>Maximum length of 18 characters</LI><LI>At least one upper-case character</LI>
    <LI>At least one lower-case character</LI><LI>At least one non-alphabetic character</LI>
    <LI>Does not contain 3 or more consecutive repeating characters (e.g. AAA)</LI>
    <LI>Does not contain the User ID</LI><LI>Does not contain common dictionary words 
    more than 4 characters long</LI><LI>New Password should not be the same as 
    previous password</LI></UL></font>

</span></li></ul>

To generate the output you want, you can write your own .ftl, or more easily avoid using the <s:actionerror/> / <s:actionmessage> tags, and do the loop by yourself:

<s:if test="actionMessages!=null && actionMessages.size > 0">
    <div class="actionmessage">
        <s:iterator value="actionMessages" >
            <span>
                <s:property escapeHtml = "false" />
            </span>
            <br/>
        </s:iterator>
    </div>
</s:if>

result:

<div class="actionmessage"><span>

    <font color="red">The password you have entered does not 
    meet password strength requirements.  Please select a new password that conforms 
    to the following standards:<UL><LI>Minimum length of 8 characters</LI>
    <LI>Maximum length of 18 characters</LI><LI>At least one upper-case character</LI>
    <LI>At least one lower-case character</LI><LI>At least one non-alphabetic character</LI>
    <LI>Does not contain 3 or more consecutive repeating characters (e.g. AAA)</LI>
    <LI>Does not contain the User ID</LI><LI>Does not contain common dictionary words 
    more than 4 characters long</LI><LI>New Password should not be the same as 
    previous password</LI></UL></font>

</span><br/></div>

You can do the loop for actionMessages, actionErrors and eventually fieldErrors and put them in a messages.jsp snippet, so you can use it in every page (without rewriting it every time) with something like:

<s:include value="/WEB-INF/content/fragments/messages.jsp" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!