使用< s:actionerror />格式化来自ResourceBundle的HTML邮件中的问题或< s:actionmessage />在Struts2中 [英] Formatting Issue in HTML messages from ResourceBundle using <s:actionerror/> or <s:actionmessage/> in Struts2

查看:139
本文介绍了使用< s:actionerror />格式化来自ResourceBundle的HTML邮件中的问题或< s:actionmessage />在Struts2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个属性文件(资源包)。我试图通过从属性文件加载它们来在jsp中显示错误。
$ b


 错误.password.rules =< font color =red>您输入的密码不符
符合密码强度要求。请选择符合以下标准的
新密码:< UL>< LI> 8个字符的最小长度< / LI>
< LI>最大长度为18个字符< / LI>< LI>至少一个大写字符< / LI>
< LI>至少一个小写字符< / LI>< LI>至少一个非字母字符< / LI>
< LI>不包含3个或更多个连续的重复字符(例如AAA)< / LI>
< LI>不包含用户ID< / LI>< LI>不包含常用词典单词
超过4个字符< / LI>< LI>新密码不应该是与
之前的密码相同< / LI>< / UL>< / font>


以上使用struts 1加载时显示如下


 您输入的密码不符合密码强度要求。 
请选择符合以下标准的新密码:




  • 最小长度为8个字符

  • 最大长度为18个字符

  • 至少一个大写字符

  • 至少一个小写字符

  • 至少一个非字母字符

  • 不包含3个或更多连续重复字符(例如AAA )

  • 不包含用户名

  • 不包含超过4个字符的常用字典单词

  • 新密码不应与以前的密码相同


加载Struts 2时显示相同见输出文件
$ b


 •您输入的密码确实不符合密码强度要求。 
请选择符合以下标准的新密码:

o最小长度为8个字符
o最大长度为18个字符
o至少一个大写字符
o至少一个小写字符
o至少一个非字母字符
o不包含3个或更多连续重复字符(例如AAA)
o不包含用户ID
o不包含长度超过4个字符的常用词典单词
o新密码不应与以前的密码相同


以上显示的是带圆圈的项目符号,分别在下一行。第一行是bullet



请建议我对属性文件所做的更改,以加载与struts 1.x相同的错误。

解决方案

< s:actionerror /> < s:actionmessage /> 标记将封装您的 actionerrors actionmessages在< ul>< li>< span> yourmessage< / span>< / li>< / ul> / p>

这会让您的讯息以圆点开始(因为它位于< li> 内),而其他使用圆形轮廓嵌套的< li> (由第二层< li> 的HTML定义) 。




$ b

 < ; s:actionmessage /> 

会生成:

 < ul class =actionMessage>< li>< span> < font color =red>您输入的密码不符合密码强度要求。请选择符合以下标准的新密码:< UL>< LI>最小长度为8个字符< / LI> < LI> 18个字符的最大长度< / LI>< LI>至少一个大写字符< / LI> < LI>至少一个小写字符< / LI>< LI>至少一个非字母字符< / LI> < LI>不包含3个或更多个连续的重复字符(例如AAA)< / LI> < LI>不包含用户ID< / LI>< LI>不包含常见词典单词超过4个字符< / LI>< LI>新密码不应与以前的密码相同< / LI> ;< / UL>< /字体>< /跨度>< /立GT;< / UL>  

< s:actionerror /> $ b

code> / < s:actionmessage> 标签,并自行完成循环: $ b

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

结果:

 < div class =actionmessage>< span> < font color =red>您输入的密码不符合密码强度要求。请选择符合以下标准的新密码:< UL>< LI>最小长度为8个字符< / LI> < LI> 18个字符的最大长度< / LI>< LI>至少一个大写字符< / LI> < LI>至少一个小写字符< / LI>< LI>至少一个非字母字符< / LI> < LI>不包含3个或更多个连续的重复字符(例如AAA)< / LI> < LI>不包含用户ID< / LI>< LI>不包含常见词典单词超过4个字符< / LI>< LI>新密码不应与以前的密码相同< / LI> ;< / UL>< /字体>< /跨度><峰; br />< / DIV>  



您可以循环执行 actionMessages actionErrors 和最终 fieldErrors 并将它们放入 messages.jsp 片段中,以便您可以在每个页面中使用它(而不必重写它)每一次)有如下所示:

 < s:include value =/ WEB-INF /content/fragments/messages.jsp/> 


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.

解决方案

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" />

这篇关于使用&lt; s:actionerror /&gt;格式化来自ResourceBundle的HTML邮件中的问题或&lt; s:actionmessage /&gt;在Struts2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆