You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nicole Luneburg <Ni...@lisasoft.com> on 2008/07/14 08:26:46 UTC

struts2 validation

Hi all!

Been looking all over the net but my validation simply isn't working :(



I have a JSP page with a dropdown box in a form.

I just want to make sure the value in the dropdown box is not empty.

It currently looks like this:



<s:form action=3D"myaction" method=3D"post" validate=3D"true">

        <s:actionerror />

        <s:fielderror />



        <s:select name=3D"fieldName"

                id=3D"fieldName"

                theme=3D"simple"

                size=3D"1"

                list=3D"fieldList"

                headerKey=3D""

                headerValue=3D"- - Please Select - -"/>

        <s:submit name=3D"Submit"/>

</s:form>



My setup is that I have an Action class, which uses a Form to set and get f= ield values from the JSP page.

In Struts1 I was using the validate(...) method in the Form class.

It seems none of the Struts2 validation examples on the net are working for=  me.

Or I'm not doing it right.



Anyone any ideas how I can do this simply?



Cheers!

nic


________________________________
The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.

Re: struts2 validation

Posted by Markus Stauffer <ma...@gmail.com>.
If you want validation like in struts1 you can implement the interface
com.opensymphony.xwork2.Validateable in your Action class. The method
validate() is the same as in your struts1 FormBean.

But maybe that's a bit old fashioned :)

Regards
-- 
Markus Stauffer

On Mon, Jul 14, 2008 at 8:54 AM, Jeromy Evans
<je...@blueskyminds.com.au> wrote:
> Nicole Luneburg wrote:
>>
>>
>>
>> <s:form action=3D"myaction" method=3D"post" validate=3D"true">
>>
>
> The validate attribute here is used to enable client-side validation only.
>  That will only work if you include the <s:head> attribute in the page.
>
>
>>
>>
>> My setup is that I have an Action class, which uses a Form to set and get
>> f= ield values from the JSP page.
>>
>> In Struts1 I was using the validate(...) method in the Form class.
>>
>> It seems none of the Struts2 validation examples on the net are working
>> for=  me.
>>
>>
>
> You haven't mentioned whether you're using XML validation or
> annotation-based validation.  If by not working you mean "does nothing",
> then your XML file is probably incorrectly named or your missing an
> annotation.  (You need to enable this separately from client-side
> validation)
>
> Whatever the case, the main difference between Struts1 and Struts2 here is
> that Struts2 performs validation on the Object, not on the form parameters.
> That means, to check that "fieldName" is non-blank, it will call
> getFieldName() after setFieldName() was called by the ParametersInterceptor.
>
> A common problem is to forget the getter, but in that case Struts will keep
> returning INPUT (validation failed) instead of invoking your action.
>
> Hope that helps,
> Jeromy Evans
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts2 validation

Posted by Markus Stauffer <ma...@gmail.com>.
Struts2 does not have FormBeans anymore. All the form fields are
instance variables directly in your Action class. Simply include a
variable fieldName of type String with getters and setters in your
Action class. The setFieldName() method will be called when you submit
the form. In the execute() and validate() methods you can use the
fieldName variable directly.

What you can also do is put a instance variable of a bean in your
Action class. You can then acces the getters and setters of this bean
in the jsp:

<s:form action="myaction">
       <s:select name="myBeanVariableName.fieldName"
                     theme="simple"
                     list="fieldList"
                     headerKey=""
                     headerValue="- - Please Select - -"/>
</s:form>

If you submit this form struts will call
myaction.getMyBeanVariableName().setFieldName(selectedValue).


Regards
-- 
Markus Stauffer


On Mon, Jul 14, 2008 at 9:16 AM, Nicole Luneburg
<Ni...@lisasoft.com> wrote:
> ps. My post added some "3D" text that isn't supposed to be there ...
>
> Thanks Jeromy.
>
> Yes I do have a <s:head> attribute, maybe not *exactly* but I have:
> <s:head theme="ajax" />
>
> Apologies for the confusion.
> What I meant in my pevious post was that I had tried a few ways to do this validation task.
> In my initial post I had described one of these attempts which I think should have worked.
>
> What I am getting regardless of what sort of validation I try to implement is this error:
>
> java.lang.StringIndexOutOfBoundsException: String index out of range: -7
>
> You mentioned the getters and setters for the fieldnames ... this only works though if the fields are stored in the action right?
> coz the fields I have aren't stored in the action, they are stored in a Form class.
>
> Ie my action looks something like:
>
> public String execute() {
>        MyForm myForm = (MyForm)super.form;
>        String myField = myForm.getFieldName();
> }
>
> Markus: Thanks for your reply too, I am trying it right now. Just want this to work grrr
>
>
> -----Original Message-----
> From: Jeromy Evans [mailto:jeromy.evans@blueskyminds.com.au]
> Sent: Monday, 14 July 2008 4:25 PM
> To: Struts Users Mailing List
> Subject: Re: struts2 validation
>
> Nicole Luneburg wrote:
>>
>>
>>
>> <s:form action=3D"myaction" method=3D"post" validate=3D"true">
>>
> The validate attribute here is used to enable client-side validation
> only.  That will only work if you include the <s:head> attribute in the
> page.
>
>
>>
>>
>> My setup is that I have an Action class, which uses a Form to set and get f= ield values from the JSP page.
>>
>> In Struts1 I was using the validate(...) method in the Form class.
>>
>> It seems none of the Struts2 validation examples on the net are working for=  me.
>>
>>
>
> You haven't mentioned whether you're using XML validation or
> annotation-based validation.  If by not working you mean "does nothing",
> then your XML file is probably incorrectly named or your missing an
> annotation.  (You need to enable this separately from client-side
> validation)
>
> Whatever the case, the main difference between Struts1 and Struts2 here
> is that Struts2 performs validation on the Object, not on the form
> parameters.
> That means, to check that "fieldName" is non-blank, it will call
> getFieldName() after setFieldName() was called by the ParametersInterceptor.
>
> A common problem is to forget the getter, but in that case Struts will
> keep returning INPUT (validation failed) instead of invoking your action.
>
> Hope that helps,
> Jeromy Evans
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: struts2 validation

Posted by Nicole Luneburg <Ni...@lisasoft.com>.
Ah!
Now I understand ... Looks like I will be changing a few things in this project ...
Thanks for your help!

-----Original Message-----
From: Dave Newton [mailto:newton.dave@yahoo.com]
Sent: Monday, 14 July 2008 10:22 PM
To: Struts Users Mailing List
Subject: RE: struts2 validation

If you don't have an action property named "fieldname" then not only will validation not work but neither will normal property copying (from form to action).

If, for example, you have a MyForm named "form", and has an appropriate getter/setter, its form element would look like this:

<s:textfield name="form.fieldName"/>

If you're handling validation programmatically (via the Validateable interface and the action's validate() method) you'd then validate against the MyForm instance, for example:

public void validate() {
    if (StringUtils.isBlank(this.form.getFieldName())) {
        ...
    }
    ...
}

It sort of seems like you're implementing a bit of ModelDriven, but manually--not a problem, but seems a bit duplicative.

Dave

--- On Mon, 7/14/08, Nicole Luneburg wrote:

> From: Nicole Luneburg <Ni...@lisasoft.com>
> Subject: RE: struts2 validation
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Date: Monday, July 14, 2008, 3:16 AM
> ps. My post added some "3D" text that isn't
> supposed to be there ...
>
> Thanks Jeromy.
>
> Yes I do have a <s:head> attribute, maybe not
> *exactly* but I have:
> <s:head theme="ajax" />
>
> Apologies for the confusion.
> What I meant in my pevious post was that I had tried a few
> ways to do this validation task.
> In my initial post I had described one of these attempts
> which I think should have worked.
>
> What I am getting regardless of what sort of validation I
> try to implement is this error:
>
> java.lang.StringIndexOutOfBoundsException: String index out
> of range: -7
>
> You mentioned the getters and setters for the fieldnames
> ... this only works though if the fields are stored in the
> action right?
> coz the fields I have aren't stored in the action, they
> are stored in a Form class.
>
> Ie my action looks something like:
>
> public String execute() {
>         MyForm myForm = (MyForm)super.form;
>         String myField = myForm.getFieldName();
> }
>
> Markus: Thanks for your reply too, I am trying it right
> now. Just want this to work grrr
>
>
> -----Original Message-----
> From: Jeromy Evans
> [mailto:jeromy.evans@blueskyminds.com.au]
> Sent: Monday, 14 July 2008 4:25 PM
> To: Struts Users Mailing List
> Subject: Re: struts2 validation
>
> Nicole Luneburg wrote:
> >
> >
> >
> > <s:form action=3D"myaction"
> method=3D"post" validate=3D"true">
> >
> The validate attribute here is used to enable client-side
> validation
> only.  That will only work if you include the
> <s:head> attribute in the
> page.
>
>
> >
> >
> > My setup is that I have an Action class, which uses a
> Form to set and get f= ield values from the JSP page.
> >
> > In Struts1 I was using the validate(...) method in the
> Form class.
> >
> > It seems none of the Struts2 validation examples on
> the net are working for=  me.
> >
> >
>
> You haven't mentioned whether you're using XML
> validation or
> annotation-based validation.  If by not working you mean
> "does nothing",
> then your XML file is probably incorrectly named or your
> missing an
> annotation.  (You need to enable this separately from
> client-side
> validation)
>
> Whatever the case, the main difference between Struts1 and
> Struts2 here
> is that Struts2 performs validation on the Object, not on
> the form
> parameters.
> That means, to check that "fieldName" is
> non-blank, it will call
> getFieldName() after setFieldName() was called by the
> ParametersInterceptor.
>
> A common problem is to forget the getter, but in that case
> Struts will
> keep returning INPUT (validation failed) instead of
> invoking your action.
>
> Hope that helps,
> Jeromy Evans
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
>
>
> The contents of this email are confidential and may be
> subject to legal or professional privilege and copyright.
> No representation is made that this email is free of
> viruses or other defects. If you have received this
> communication in error, you may not copy or distribute any
> part of it or otherwise disclose its contents to anyone.
> Please advise the sender of your incorrect receipt of this
> correspondence.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: struts2 validation

Posted by Nicole Luneburg <Ni...@lisasoft.com>.
In case it helps anyone else, I found a simple solution ... Annotations.
I did exactly what is here:
http://struts.apache.org/2.x/docs/validation-annotation.html

Thanks again for everyone's help!


The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: struts2 validation

Posted by Dave Newton <ne...@yahoo.com>.
If you don't have an action property named "fieldname" then not only will validation not work but neither will normal property copying (from form to action).

If, for example, you have a MyForm named "form", and has an appropriate getter/setter, its form element would look like this:

<s:textfield name="form.fieldName"/>

If you're handling validation programmatically (via the Validateable interface and the action's validate() method) you'd then validate against the MyForm instance, for example:

public void validate() {
    if (StringUtils.isBlank(this.form.getFieldName())) {
        ...
    }
    ...
}

It sort of seems like you're implementing a bit of ModelDriven, but manually--not a problem, but seems a bit duplicative.

Dave

--- On Mon, 7/14/08, Nicole Luneburg wrote:

> From: Nicole Luneburg <Ni...@lisasoft.com>
> Subject: RE: struts2 validation
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Date: Monday, July 14, 2008, 3:16 AM
> ps. My post added some "3D" text that isn't
> supposed to be there ...
> 
> Thanks Jeromy.
> 
> Yes I do have a <s:head> attribute, maybe not
> *exactly* but I have:
> <s:head theme="ajax" />
> 
> Apologies for the confusion.
> What I meant in my pevious post was that I had tried a few
> ways to do this validation task.
> In my initial post I had described one of these attempts
> which I think should have worked.
> 
> What I am getting regardless of what sort of validation I
> try to implement is this error:
> 
> java.lang.StringIndexOutOfBoundsException: String index out
> of range: -7
> 
> You mentioned the getters and setters for the fieldnames
> ... this only works though if the fields are stored in the
> action right?
> coz the fields I have aren't stored in the action, they
> are stored in a Form class.
> 
> Ie my action looks something like:
> 
> public String execute() {
>         MyForm myForm = (MyForm)super.form;
>         String myField = myForm.getFieldName();
> }
> 
> Markus: Thanks for your reply too, I am trying it right
> now. Just want this to work grrr
> 
> 
> -----Original Message-----
> From: Jeromy Evans
> [mailto:jeromy.evans@blueskyminds.com.au]
> Sent: Monday, 14 July 2008 4:25 PM
> To: Struts Users Mailing List
> Subject: Re: struts2 validation
> 
> Nicole Luneburg wrote:
> >
> >
> >
> > <s:form action=3D"myaction"
> method=3D"post" validate=3D"true">
> >
> The validate attribute here is used to enable client-side
> validation
> only.  That will only work if you include the
> <s:head> attribute in the
> page.
> 
> 
> >
> >
> > My setup is that I have an Action class, which uses a
> Form to set and get f= ield values from the JSP page.
> >
> > In Struts1 I was using the validate(...) method in the
> Form class.
> >
> > It seems none of the Struts2 validation examples on
> the net are working for=  me.
> >
> >
> 
> You haven't mentioned whether you're using XML
> validation or
> annotation-based validation.  If by not working you mean
> "does nothing",
> then your XML file is probably incorrectly named or your
> missing an
> annotation.  (You need to enable this separately from
> client-side
> validation)
> 
> Whatever the case, the main difference between Struts1 and
> Struts2 here
> is that Struts2 performs validation on the Object, not on
> the form
> parameters.
> That means, to check that "fieldName" is
> non-blank, it will call
> getFieldName() after setFieldName() was called by the
> ParametersInterceptor.
> 
> A common problem is to forget the getter, but in that case
> Struts will
> keep returning INPUT (validation failed) instead of
> invoking your action.
> 
> Hope that helps,
> Jeromy Evans
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 
> The contents of this email are confidential and may be
> subject to legal or professional privilege and copyright.
> No representation is made that this email is free of
> viruses or other defects. If you have received this
> communication in error, you may not copy or distribute any
> part of it or otherwise disclose its contents to anyone.
> Please advise the sender of your incorrect receipt of this
> correspondence.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: struts2 validation

Posted by Nicole Luneburg <Ni...@lisasoft.com>.
ps. My post added some "3D" text that isn't supposed to be there ...

Thanks Jeromy.

Yes I do have a <s:head> attribute, maybe not *exactly* but I have:
<s:head theme="ajax" />

Apologies for the confusion.
What I meant in my pevious post was that I had tried a few ways to do this validation task.
In my initial post I had described one of these attempts which I think should have worked.

What I am getting regardless of what sort of validation I try to implement is this error:

java.lang.StringIndexOutOfBoundsException: String index out of range: -7

You mentioned the getters and setters for the fieldnames ... this only works though if the fields are stored in the action right?
coz the fields I have aren't stored in the action, they are stored in a Form class.

Ie my action looks something like:

public String execute() {
        MyForm myForm = (MyForm)super.form;
        String myField = myForm.getFieldName();
}

Markus: Thanks for your reply too, I am trying it right now. Just want this to work grrr


-----Original Message-----
From: Jeromy Evans [mailto:jeromy.evans@blueskyminds.com.au]
Sent: Monday, 14 July 2008 4:25 PM
To: Struts Users Mailing List
Subject: Re: struts2 validation

Nicole Luneburg wrote:
>
>
>
> <s:form action=3D"myaction" method=3D"post" validate=3D"true">
>
The validate attribute here is used to enable client-side validation
only.  That will only work if you include the <s:head> attribute in the
page.


>
>
> My setup is that I have an Action class, which uses a Form to set and get f= ield values from the JSP page.
>
> In Struts1 I was using the validate(...) method in the Form class.
>
> It seems none of the Struts2 validation examples on the net are working for=  me.
>
>

You haven't mentioned whether you're using XML validation or
annotation-based validation.  If by not working you mean "does nothing",
then your XML file is probably incorrectly named or your missing an
annotation.  (You need to enable this separately from client-side
validation)

Whatever the case, the main difference between Struts1 and Struts2 here
is that Struts2 performs validation on the Object, not on the form
parameters.
That means, to check that "fieldName" is non-blank, it will call
getFieldName() after setFieldName() was called by the ParametersInterceptor.

A common problem is to forget the getter, but in that case Struts will
keep returning INPUT (validation failed) instead of invoking your action.

Hope that helps,
Jeromy Evans





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts2 validation

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Nicole Luneburg wrote:
>
>
>
> <s:form action=3D"myaction" method=3D"post" validate=3D"true">
>   
The validate attribute here is used to enable client-side validation 
only.  That will only work if you include the <s:head> attribute in the 
page.


>
>
> My setup is that I have an Action class, which uses a Form to set and get f= ield values from the JSP page.
>
> In Struts1 I was using the validate(...) method in the Form class.
>
> It seems none of the Struts2 validation examples on the net are working for=  me.
>
>   

You haven't mentioned whether you're using XML validation or 
annotation-based validation.  If by not working you mean "does nothing", 
then your XML file is probably incorrectly named or your missing an 
annotation.  (You need to enable this separately from client-side 
validation)

Whatever the case, the main difference between Struts1 and Struts2 here 
is that Struts2 performs validation on the Object, not on the form 
parameters.
That means, to check that "fieldName" is non-blank, it will call 
getFieldName() after setFieldName() was called by the ParametersInterceptor.

A common problem is to forget the getter, but in that case Struts will 
keep returning INPUT (validation failed) instead of invoking your action.

Hope that helps,
Jeromy Evans





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: struts2 validation

Posted by Nicole Luneburg <Ni...@lisasoft.com>.
Hi Pierre,

Thanks for your code.
Yes I did get it working with annotation, phew!

Nicole

-----Original Message-----
From: Pierrot52 [mailto:pierre.lasante@gmail.com]
Sent: Wednesday, 16 July 2008 9:48 PM
To: user@struts.apache.org
Subject: Re: struts2 validation


Hi Nicole,

This is an action class that does validation of two parameters:

public class SignonAction extends ActionSupport {
    private String username;
    private String password;

    @Override
    public String execute() throws Exception {
        Map session = null;
        boolean authenticated = false;
        try {
            authenticated = Utils.authenticate(username,password);
        } catch(Exception e) {
            // Swallow the exception
        }
        if (authenticated) {
            session = ActionContext.getContext().getSession();
            User user = new User();
            user.setLogin(username);
            user.setEmail("set your email");
            session.put("loggedIn", user);
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    @RequiredStringValidator(message="", key = "username.required")
    @StringLengthFieldValidator(message = "", key =
"username.invalidLength", minLength = "6" , maxLength = "15")
    @RegexFieldValidator(message = "", key = "prompt.invalidCharacters",
expression = "^[a-zA-Z0-9]+$")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @RequiredStringValidator(message="", key="password.required")
    @StringLengthFieldValidator(message = "", key =
"password.invalidLength", minLength = "6" , maxLength = "15")
    @RegexFieldValidator(message = "", key = "prompt.invalidCharacters",
expression = "^[a-zA-Z0-9]+$")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

It uses annotation to validate.

Hope this will help.

Regards.

Pierre


Nicole Luneburg wrote:
>
> Hi all!
>
> Been looking all over the net but my validation simply isn't working :(
>
>
>
> I have a JSP page with a dropdown box in a form.
>
> I just want to make sure the value in the dropdown box is not empty.
>
> It currently looks like this:
>
>
>
> <s:form action=3D"myaction" method=3D"post" validate=3D"true">
>
>         <s:actionerror />
>
>         <s:fielderror />
>
>
>
>         <s:select name=3D"fieldName"
>
>                 id=3D"fieldName"
>
>                 theme=3D"simple"
>
>                 size=3D"1"
>
>                 list=3D"fieldList"
>
>                 headerKey=3D""
>
>                 headerValue=3D"- - Please Select - -"/>
>
>         <s:submit name=3D"Submit"/>
>
> </s:form>
>
>
>
> My setup is that I have an Action class, which uses a Form to set and get
> f= ield values from the JSP page.
>
> In Struts1 I was using the validate(...) method in the Form class.
>
> It seems none of the Struts2 validation examples on the net are working
> for=  me.
>
> Or I'm not doing it right.
>
>
>
> Anyone any ideas how I can do this simply?
>
>
>
> Cheers!
>
> nic
>
>
> ________________________________
> The contents of this email are confidential and may be subject to legal or
> professional privilege and copyright. No representation is made that this
> email is free of viruses or other defects. If you have received this
> communication in error, you may not copy or distribute any part of it or
> otherwise disclose its contents to anyone. Please advise the sender of
> your incorrect receipt of this correspondence.
>
>

--
View this message in context: http://www.nabble.com/struts2-validation-tp18438182p18485869.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts2 validation

Posted by Pierrot52 <pi...@gmail.com>.
Hi Nicole,

This is an action class that does validation of two parameters:

public class SignonAction extends ActionSupport {
    private String username;
    private String password;
    
    @Override
    public String execute() throws Exception {
        Map session = null;
        boolean authenticated = false;
        try {
            authenticated = Utils.authenticate(username,password);
        } catch(Exception e) {
            // Swallow the exception
        }
        if (authenticated) {
            session = ActionContext.getContext().getSession();
            User user = new User();
            user.setLogin(username);
            user.setEmail("set your email");
            session.put("loggedIn", user);
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
    
    @RequiredStringValidator(message="", key = "username.required")
    @StringLengthFieldValidator(message = "", key =
"username.invalidLength", minLength = "6" , maxLength = "15")
    @RegexFieldValidator(message = "", key = "prompt.invalidCharacters",
expression = "^[a-zA-Z0-9]+$")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @RequiredStringValidator(message="", key="password.required")
    @StringLengthFieldValidator(message = "", key =
"password.invalidLength", minLength = "6" , maxLength = "15")
    @RegexFieldValidator(message = "", key = "prompt.invalidCharacters",
expression = "^[a-zA-Z0-9]+$")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

It uses annotation to validate.

Hope this will help.

Regards.

Pierre


Nicole Luneburg wrote:
> 
> Hi all!
> 
> Been looking all over the net but my validation simply isn't working :(
> 
> 
> 
> I have a JSP page with a dropdown box in a form.
> 
> I just want to make sure the value in the dropdown box is not empty.
> 
> It currently looks like this:
> 
> 
> 
> <s:form action=3D"myaction" method=3D"post" validate=3D"true">
> 
>         <s:actionerror />
> 
>         <s:fielderror />
> 
> 
> 
>         <s:select name=3D"fieldName"
> 
>                 id=3D"fieldName"
> 
>                 theme=3D"simple"
> 
>                 size=3D"1"
> 
>                 list=3D"fieldList"
> 
>                 headerKey=3D""
> 
>                 headerValue=3D"- - Please Select - -"/>
> 
>         <s:submit name=3D"Submit"/>
> 
> </s:form>
> 
> 
> 
> My setup is that I have an Action class, which uses a Form to set and get
> f= ield values from the JSP page.
> 
> In Struts1 I was using the validate(...) method in the Form class.
> 
> It seems none of the Struts2 validation examples on the net are working
> for=  me.
> 
> Or I'm not doing it right.
> 
> 
> 
> Anyone any ideas how I can do this simply?
> 
> 
> 
> Cheers!
> 
> nic
> 
> 
> ________________________________
> The contents of this email are confidential and may be subject to legal or
> professional privilege and copyright. No representation is made that this
> email is free of viruses or other defects. If you have received this
> communication in error, you may not copy or distribute any part of it or
> otherwise disclose its contents to anyone. Please advise the sender of
> your incorrect receipt of this correspondence.
> 
> 

-- 
View this message in context: http://www.nabble.com/struts2-validation-tp18438182p18485869.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org