Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

DynaActionForm  and  field checking

Thread view: 
gk - 16 Nov 2005 05:34 GMT
when i use  Form Bean  i use ActionErrors to validate

example :

public final class HelloForm extends ActionForm {

public ActionErrors validate(ActionMapping mapping,
                                HttpServletRequest request) {

       ActionErrors errors = new ActionErrors();

       if ((person == null) || (person.length() < 1))
           errors.add("person", new
ActionError("ch03.hello.no.person.error"));

       return errors;
   }

But when if i use DynaActionForm  how do i validate ?

i DONT  want to use Struts validation.xml + validation-rules.xml.

how do i do field  checking  if there is DynaActionForm ?
Tim B - 16 Nov 2005 05:57 GMT
> when i use  Form Bean  i use ActionErrors to validate
<snip example>

> But when if i use DynaActionForm  how do i validate ?
>
> i DONT  want to use Struts validation.xml + validation-rules.xml.
>
> how do i do field  checking  if there is DynaActionForm ?

you could do validation in your action
gk - 16 Nov 2005 08:53 GMT
you mean i could place this  code in the Action class  ?

public ActionErrors validate(ActionMapping mapping,
                                HttpServletRequest request) {

       ActionErrors errors = new ActionErrors();

       if ((person == null) || (person.length() < 1))
           errors.add("person", new
ActionError("ch03.hello.no.person.error"));

       return errors;
   }

another question :  should we use Struts validation.xml  ?  some people
say its good ...some says it is not. what do you think ?

is this true Struts also can do Client-side validation ??  !! ...how is
that possible ? only javascript (no other language) does client side
validation .  How much correct it is ?
gk - 16 Nov 2005 10:28 GMT
anybody know Struts ?
Roedy Green - 16 Nov 2005 12:47 GMT
>anybody know Struts ?

there must be at least eight.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 16 Nov 2005 12:48 GMT
On Wed, 16 Nov 2005 12:47:31 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :

>there must be at least eight.

that is a punch line to an old joke.

"Think of all the people in the world worse off than you are  ---"
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Tim B - 16 Nov 2005 18:37 GMT
> you mean i could place this  code in the Action class  ?
>
[quoted text clipped - 9 lines]
>         return errors;
>     }

yes you could do something like that  but you would have to call it from the
'execute' method of your action, or just do it in the execute method, like
this:

public final ActionForward execute(
       final ActionMapping mapping,
       final ActionForm form,
       final HttpServletRequest request,
       final HttpServletResponse response) throws Exception {

MyFormBeanType myFormBean = (MyFormBeanType)form;

   // note use of ActionMessages to avoid using the
   // deprecated 'saveErrors(HttpServletRequest, ActionMessages)'
ActionMessages messages = new ActionMessages();

if ((myFormBean.getPerson() == null) || (myFormBean.getPerson().length() <
1)) {
    messages.add("person", new
ActionMessage("ch03.hello.no.person.error"));
}

   //maybe validate some additional fields here

 if(messages.isEmpty()) {
      // do normal stuff here
      return mapping.findForward("success");
} else {
     this.saveErrors(request, messages);
     return mapping.findForward("failure");
}
}

> another question :  should we use Struts validation.xml  ?  some people
> say its good ...some says it is not. what do you think ?

It's ok if you have forms with many fields that don't require complex
validation. I personally don't like it much, one reason being that  there
isn't adequate syntax checking of the validation.xml  file. If you have an
error in the xml, in some cases the field being validated will always pass
validation, with no other indication that something is wrong.

> is this true Struts also can do Client-side validation ??  !! ...how is
> that possible ? only javascript (no other language) does client side
> validation .  How much correct it is ?

The Struts validator uses javascript to do client side validation, and
client side is optional.
I think you could use a good Struts book. Check out "Struts in Action" by
Ted Husted
gk - 17 Nov 2005 06:34 GMT
great answers. you are definitely a Struts guru.

> > you mean i could place this  code in the Action class  ?
> >
[quoted text clipped - 60 lines]
> I think you could use a good Struts book. Check out "Struts in Action" by
> Ted Husted
Abhijat Vatsyayan - 16 Nov 2005 15:43 GMT
I would think you can use org.apache.struts.validator.DynaValidatorForm
Abhijat

> when i use  Form Bean  i use ActionErrors to validate
>
[quoted text clipped - 19 lines]
>
> how do i do field  checking  if there is DynaActionForm ?
Abhijat Vatsyayan - 16 Nov 2005 16:00 GMT
I think I made is sound simpler than it is (I have not used struts for
development). If you do custom processing for your forms, you have to
write custom code. This code has to work with DynaActionForm. So here
are two approaches I can suggest  -

* Write a separate subclass of DynaValidatorForm for each validation. I
have a feeling you do not want to do this.

* Subclass DynaValidatorForm, separate out the validation part (probably
use a strategy) and read the name of this Validator class through
configuration. You can add a property "ValidatorClass" - pass the name
of the concrete validator class through the "form-property" element. The
 validate method can create the correct Validator class (by reading the
property "ValidatorClass") and delegate the work to this object.

The documentation for DynaValidatorForm says something about "basic
field validation based on an XML file". You might also want to look into
that (or wait for the eight people who know struts to post a reply).
Abhijat

> I would think you can use org.apache.struts.validator.DynaValidatorForm
> Abhijat
[quoted text clipped - 22 lines]
>>
>> how do i do field  checking  if there is DynaActionForm ?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.