Why are my actionErrors and fieldErrors displayed with braces []?

时光总嘲笑我的痴心妄想 提交于 2021-01-28 09:54:32

问题


Once the pages have captured errors within my Struts 2 application the errors are displayed correctly from my resource bundles however they are always displayed with a braces [] For example

[first name must not be empty]

These are displayed through the tags

<s:actionerror />

<s:iterator value="fieldErrors">
   <s:property value="value" />
</s:iterator>

These come through the Action-validation.xml and setting a addFieldError() through the validation method

Is this something to do with my theme being set to simple ? I can't see anything in documentation or anything through other posts...


回答1:


Why

No, it is not about theme. Take a look at getFieldErrors method. It returns map where value is a list of strings.

public Map<String, List<String>> getFieldErrors() {
    return validationAware.getFieldErrors();
}

Your code iterates over map and value is a list. List as string will be displayed with surrounding braces.

How

If you need only value of the error message and not the key then you can use addActionError method instead and iterate over actionErrors.

If you want to iterate fieldErrors then you can do it like that.

<s:iterator value="fieldErrors">
    <s:property value="key"/>:
    <s:iterator value="value">
        <s:property/>
    </s:iterator>
</s:iterator>

And of course there are tags for displaying errors/messages, see Non-Form UI Tags.




回答2:


Without default xhtml theme it would render objects as it's printed toString(), i.e. formatting an array with brackets.

Render field errors if they exists the specific layout of the rendering depends on the theme itself. Empty (null or blank string) errors will not be printed. The field error strings will be html escaped by default.


The code to display errors should look like

<s:if test="hasFieldErrors()">
   <div class="errors">
      <s:fielderror />
   </div>
</s:if>



来源:https://stackoverflow.com/questions/44948426/why-are-my-actionerrors-and-fielderrors-displayed-with-braces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!