问题
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