Formatting Issue in HTML messages from ResourceBun

2019-03-03 18:13发布

I have a properties file (resource bundle). I am trying to show errors in one of the jsp by loading them from properties file. I have an entry as below in my properties file.

errors.password.rules=<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>

The above when loaded using struts 1 shows as below

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

The same when loaded with Struts 2 shows see attached file for output

• The password you have entered does not meet password strength requirements. 
  Please select a new password that conforms to the following standards:

o Minimum length of 8 characters 
o Maximum length of 18 characters 
o At least one upper-case character 
o At least one lower-case character 
o At least one non-alphabetic character 
o Does not contain 3 or more consecutive repeating characters (e.g. AAA) 
o Does not contain the User ID 
o Does not contain common dictionary words more than 4 characters long 
o New Password should not be the same as previous password

The above is displaying with the circled bullet, each in a next line. And for bullet is coming for first line

Please suggest what changes i have to do to my properties files to load the errors same as with struts 1.x.

1条回答
爷的心禁止访问
2楼-- · 2019-03-03 19:10

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" />
查看更多
登录 后发表回答