struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Lindholm <greg.lindh...@gmail.com>
Subject Re: Struts 2, validation and s:actionerror
Date Mon, 26 Oct 2009 15:23:33 GMT
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@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-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message