|
View:
New views
12 Messages
—
Rating Filter:
Alert me
|
|
|
Struts 2, validation and s:actionerrorHi 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-unsubscribe@... For additional commands, e-mail: user-help@... |
|
|
Re: Struts 2, validation and s:actionerrorThere 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.ballantyne@...> 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-unsubscribe@... > For additional commands, e-mail: user-help@... > > |
|
|
Re: Struts 2, validation and s:actionerrorOkay 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.lindholm@...>: > 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.ballantyne@...> 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-unsubscribe@... >> For additional commands, e-mail: user-help@... >> >> > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
|
|
[Struts 2] Dynamic content of a tooltip for internationalizationHi,
How do you render the content of a tooltip dynamic ? I am using several messages_XX.properties files, one for each language. In each properties file, I have defined the key blabla.tooltip. For instance in messages_en.properties: blabla.tooltip=Pay before the end of the term. In messages_fr.properties: blabla.tooltip=Payez avant la fin du contrat. <s:textfield name="blabla" key="pay.blabla" tooltip="Pay before the end of the term."/> Question : how do i affect the content of my key blabla.tooltip to the content of tooltip ? Thanks for helping. --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
|
|
Re: Struts 2, validation and s:actionerrorSo, 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.ballantyne@...> 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.lindholm@...>: > > 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.ballantyne@...> 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-unsubscribe@... >>> For additional commands, e-mail: user-help@... >>> >>> >>> >> > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscribe@... > For additional commands, e-mail: user-help@... > > |
|
|
Re: [Struts 2] Dynamic content of a tooltip for internationalization<s:textfield name="blabla" key="pay.blabla"
tooltip="%{getText('blabla.tooltip')}"/> On Tue, Oct 27, 2009 at 8:16 AM, Fernandes Celinio < cfernandes@...> wrote: > Hi, > > How do you render the content of a tooltip dynamic ? > I am using several messages_XX.properties files, one for each language. > > In each properties file, I have defined the key blabla.tooltip. > > For instance in messages_en.properties: > blabla.tooltip=Pay before the end of the term. > > In messages_fr.properties: > blabla.tooltip=Payez avant la fin du contrat. > > <s:textfield name="blabla" key="pay.blabla" tooltip="Pay > before the end of the term."/> > > Question : how do i affect the content of my key blabla.tooltip to the > content of tooltip ? > > Thanks for helping. > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscribe@... > For additional commands, e-mail: user-help@... > > |
|
|
Re: Struts 2, validation and s:actionerrorThanks Greg, that does the trick. I am now able to display the
validation errors at the top of the form. Quoting Greg Lindholm <greg.lindholm@...>: > 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.ballantyne@...> 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.lindholm@...>: >> >> 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.ballantyne@...> 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-unsubscribe@... >>>> For additional commands, e-mail: user-help@... >>>> >>>> >>>> >>> >> >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: user-unsubscribe@... >> For additional commands, e-mail: user-help@... >> >> > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
|
|
Re: Struts 2, validation and s:actionerrorHi Greg,
i was about writing that one should read iterator tag docs because one can use maps in s:iterator value, but instead i've just read it myself and the info about iterating maps is not there. I use s:iterator where value is a map and it works in 2.1.6 just adding key or value inside iterator do the job. So i'm a little confused, is it ok or not to iterate over a map? Your example should also work like this: <s:iterator value="getFieldErrors()"> <s:iterator value="value"> Field Name = <s:property value="key"/> Message = <s:property value="%{top}"/><br> </s:iterator> </s:iterator> Best greetings, Paweł Wielgus. 2009/10/27 Greg Lindholm <greg.lindholm@...>: > 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.ballantyne@...> 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.lindholm@...>: >> >> 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.ballantyne@...> 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-unsubscribe@... >>>> For additional commands, e-mail: user-help@... >>>> >>>> >>>> >>> >> >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: user-unsubscribe@... >> For additional commands, e-mail: user-help@... >> >> > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
|
|
Re: Struts 2, validation and s:actionerrorHey, you are correct.
Just dove into the code and I found that is uses a MakeIterator utility class that if given a Map will call entrySet(). Maybe a commiter will be kind enough update the iterator tag doc to include a Map example and a note on how Map is converted to an Iterator. 2009/10/27 Paweł Wielgus <poulwiel@...> > Hi Greg, > i was about writing that one should read iterator tag docs > because one can use maps in s:iterator value, > but instead i've just read it myself and the info about iterating maps > is not there. > I use s:iterator where value is a map and it works in 2.1.6 > just adding key or value inside iterator do the job. > > So i'm a little confused, is it ok or not to iterate over a map? > Your example should also work like this: > > <s:iterator value="getFieldErrors()"> > <s:iterator value="value"> > Field Name = <s:property value="key"/> > Message = <s:property value="%{top}"/><br> > </s:iterator> > </s:iterator> > > Best greetings, > Paweł Wielgus. > > > 2009/10/27 Greg Lindholm <greg.lindholm@...>: > > 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.ballantyne@...> 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.lindholm@...>: > >> > >> 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.ballantyne@...> 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-unsubscribe@... > >>>> For additional commands, e-mail: user-help@... > >>>> > >>>> > >>>> > >>> > >> > >> > >> > >> --------------------------------------------------------------------- > >> To unsubscribe, e-mail: user-unsubscribe@... > >> For additional commands, e-mail: user-help@... > >> > >> > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscribe@... > For additional commands, e-mail: user-help@... > > |
|
|
Re: Struts 2, validation and s:actionerrorMore info for the committer:
It looks like the javadoc on org.apache.struts2.components.IteratorComponent contains info that Map is supported but it is not getting into the tag doc perhaps because the <!-- END SNIPPET: javadoc --> is messed up and not on it's own line. Also a note that a Map will iterate on the entrySet() would be very helpful along with an example. |
|
|
RE: [Struts 2] Dynamic content of a tooltip for internationalizationThanks, it works of course :)
-----Message d'origine----- De : Greg Lindholm [mailto:greg.lindholm@...] Envoyé : mardi 27 octobre 2009 14:42 À : Struts Users Mailing List Objet : Re: [Struts 2] Dynamic content of a tooltip for internationalization <s:textfield name="blabla" key="pay.blabla" tooltip="%{getText('blabla.tooltip')}"/> On Tue, Oct 27, 2009 at 8:16 AM, Fernandes Celinio < cfernandes@...> wrote: > Hi, > > How do you render the content of a tooltip dynamic ? > I am using several messages_XX.properties files, one for each language. > > In each properties file, I have defined the key blabla.tooltip. > > For instance in messages_en.properties: > blabla.tooltip=Pay before the end of the term. > > In messages_fr.properties: > blabla.tooltip=Payez avant la fin du contrat. > > <s:textfield name="blabla" key="pay.blabla" tooltip="Pay > before the end of the term."/> > > Question : how do i affect the content of my key blabla.tooltip to the > content of tooltip ? > > Thanks for helping. > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscribe@... > For additional commands, e-mail: user-help@... > > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
|
|
Re: Struts 2, validation and s:actionerrorThe code below works a treat but just to help anyone out who might be
trying to do the same thing you can also use the tag <s:fielderror /> which does a similar thing but without the fine control on the formatting. Quoting Greg Lindholm <greg.lindholm@...>: > 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> > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
| Free embeddable forum powered by Nabble | Forum Help |