So, fieldErrors is a Map<String, List<String>>, the key is the field name
and the value is a List<String> of messages.

I don't think you can use <s:iterator value="fieldErrors"> directly on a Map
as a Map is not iteratable (check the iterator tag doc [1]).

I think you will need to iterate on either the entry set <s:iterator
value="getFieldErrors().entrySet()"> or the key set <s:iterator
value="getFieldErrors().keySet()">.

As for %{top}, this gives you the top object on the value stack which inside
the <s:iterator > tag is the current object in the iteration, this is just
short-hand instead of using the var= attribute.

You could try something like this

<s:iterator value="getFieldErrors().entrySet()" var="entry">
     <s:iterator value="#entry.value">
           Field Name = <s:property value="#entry.key"/>
           Message = <s:property value="%{top}"/><br>
     </s:iterator>
</s:iterator>

[1] http://struts.apache.org/2.x/docs/iterator.html

On Tue, Oct 27, 2009 at 4:59 AM, carl ballantyne <
carl.ballant...@cast-info.es> wrote:

> Okay that makes sense. Thanks for that!
>
> But your example used a hardcoded fieldname. In order to dynamically loop
> the fieldnames and display the errors for each of those fields I have tried:
>
> <s:iterator value="fieldErrors" var="fieldNameErrors">
>
>        <s:iterator value="%{fieldNameErrors}" var="fieldError">
>
>        ERROR1:<s:property value="%{top}"/><br>
>        ERROR2 <s:property value="#fieldError"/><br>
>        </s:iterator>
> </s:iterator>
>
> But it always outputs the whole list, ie firstname=[You must enter a
> firstname1., You must enter a firstname2] and not
> You must enter a firstname1
> You must enter a firstname2.
>
> %{fieldNameErrors} is being passed as an object to the second iterator and
> it is a List. I am a little confused by the whole #fieldNameErrors,
> %{fieldNameErros} syntax. And where is %{top} coming from? I don't see
> anything in the doc for the tag -
> http://struts.apache.org/2.1.8/docs/iterator.html.
>
> Cheers,
> Carl.
>
>
>
>
>
>
> Quoting Greg Lindholm <greg.lindh...@gmail.com>:
>
>  There are three buckets for error messages; general errors, general
>> messages
>> and field errors.
>> <s:actionerror /> and <s:actionmessage /> display the general errors and
>> general messages.
>> Most validation will product field errors which by default get rendered
>> beside the field with the error.
>>
>> You can control how field errors get displayed which is common if you use
>> the Simple Theme.
>>
>> Here is a snippet for manually displaying field errors for field 'name' in
>> this case:
>>
>> <s:iterator value="fieldErrors['name']">
>> <tr><td></td><td><span class="errorMessage"><s:property
>> value="%{top}"/></span></td></tr>
>> </s:iterator>
>>
>> The object fieldErrors (Map<String, List<String>>) is provided by
>> ActionSupport [1] via ValidationAwareSupport [2], the key is the field
>> name.
>>
>> [1]
>>
>> http://struts.apache.org/2.x/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html
>> [2]
>>
>> http://struts.apache.org/2.x/struts2-core/apidocs/com/opensymphony/xwork2/ValidationAwareSupport.html
>>
>>
>> On Mon, Oct 26, 2009 at 10:57 AM, carl ballantyne <
>> carl.ballant...@cast-info.es> wrote:
>>
>>  Hi Everyone,
>>>
>>> I am new to Struts 2 and trying to make the switch from Struts 1.
>>>
>>> I cannot figure out how to get the validation to display the
>>> messages/errors in the input form with the use of the s:actionerror or
>>> s:actionmessage tags. It never outputs anything despite the validation
>>> appearing to work.
>>>
>>> I have searched and searched and cannot find an example of this anywhere.
>>> I
>>> simple want the user to be directed back to the form if there are errors
>>> and
>>> display all errors at the top of the form.
>>>
>>> What I have below is displaying field specific error message beside the
>>> field in the form with struts 2 tags ... which is not what I need.
>>>
>>> I am using Struts 2.1.8.
>>>
>>> I have the following in my jsp.
>>>
>>> errors
>>> <s:actionerror />
>>>
>>> messages
>>> <s:actionmessage />
>>>
>>> <h2>Form with Struts 2 tags.</h2>
>>> <s:form action="simpleAction" validate="true">
>>>       <s:textfield label="firstname" name="firstname"></s:textfield>
>>>       <s:submit label="Save" name="Save"></s:submit>
>>> </s:form>
>>>
>>>
>>> <h2>Form without Struts 2 tags.</h2>
>>> <form action="<s:url action="simpleAction"/>" method="post">
>>>       <input type="text" name="firstname"><br>
>>>       <input type="submit" value="Save"/>
>>> </form>
>>>
>>> And my action class:
>>>
>>> public class SimpleAction extends ActionSupport {
>>>       static Logger logger = Logger.getLogger(SimpleAction.class);
>>>
>>>       private String firstname;
>>>
>>>       public String getFirstname() {
>>>               return this.firstname;
>>>       }
>>>       public void setFirstname(String firstname) {
>>>               this.firstname = firstname;
>>>       }
>>>
>>>       public String execute() throws Exception {
>>>
>>>               logger.debug("execute SimpleAction");
>>>               return SUCCESS;
>>>       }
>>>
>>> }
>>>
>>>
>>> And my struts.xml
>>>
>>> <action name="simpleAction" class="com.myapp.action.SimpleAction">
>>>  <result name="success">/WEB-INF/jsp/simpleActionSuccess.jsp</result>
>>>  <result name="input">/WEB-INF/jsp/simpleActionForm.jsp</result>
>>> </action>
>>>
>>>
>>> And my SimpleAction-validation.xml which is in the same directory as the
>>> action class.
>>>
>>> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
>>> 1.0.2//EN"
>>>      "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd";>
>>> <validators>
>>>  <field name="firstname">
>>>       <field-validator type="requiredstring">
>>>       <message>You must enter a firstname</message>
>>>   </field-validator>
>>>  </field>
>>> </validators>
>>>
>>>
>>> Cheers,
>>> Carl.
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
>>> For additional commands, e-mail: user-h...@struts.apache.org
>>>
>>>
>>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>
>

Reply via email to