You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by kimbuba <ki...@yahoo.it> on 2004/05/11 22:39:51 UTC

ValidatorForm exception handling

Hello!

Here is my problem:

On ActionForm validate method i have a BusinessDelagate who access some DAO
resources.

Because of that, probably my businessdelegate will throw some kind of
exceptions i.e. MyDAOException.
Is there any kind of solution to handle such exceptions?

On Actions i can catch my exceptions to appropriate global-exception
handlers.

Because  validate(ActionMapping mapping,HttpServletRequest request) doesn't
throw any exception i can't
go with an overwritten validate with an exception to be thrown.
If i try with a RuntimeException it will break with an "internal error"
(tomcat)

Any ideas?
Thnax!




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


Re: ValidatorForm exception handling

Posted by kimbuba <ki...@yahoo.it>.
I see what you think:
1) do common validation stuff (required..)
2) if it pass go to action execute
3) do others validation with BusinessDelegate
4) -> success OR go back with errors: return mapping.getInputForward();

the problem is different.
If validator finds some errors it will go back to input parameter.
In this case i would load a bean from DB and put it on the request.
I.E. "Hey mr. Foo you have those problems: "
i have to fetch Foo bean from db in case of basic error to display a custom
message.
How can i do that?
The only place i've found is on the validate method.

Thnx a lot!




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


Re: ValidatorForm exception handling

Posted by kimbuba <ki...@yahoo.it>.
> Now as you say, the validate method of the ActionForm doesn't handle
> exceptions. So my suggestion was to move your validation out of the
> ActionForm's validate method and into the Action's execute method. That
way
> if your DAO throws an exception, you could handle it using the struts
> exception handling.

that is what i meen.
That did the job!
Sorry for my english.

Thnx a lot for your time
kimbuba.




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


Re: ValidatorForm exception handling

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
I'm not sure I understand you, but here goes...

When a request "hits" Struts, struts processes it with a "RequestProcessor",
the "standard" RequestProcessor does a number of things, the main steps
being:

* Either get or create the ActionForm associated with the mapping
* Populate the ActionForm from the request
* Call the ActionForm's validate method (if validate="true" in the mapping)
* Call the Action's execute method
* Forward Processing

Now as you say, the validate method of the ActionForm doesn't handle
exceptions. So my suggestion was to move your validation out of the
ActionForm's validate method and into the Action's execute method. That way
if your DAO throws an exception, you could handle it using the struts
exception handling.

>From what you say though, its not the actual validation that might throw the
exception, its when you want the DAO to pass back some info in the event of
a validation error. So how about this. Set your mapping to
validate="false" - that way the RequestProcessor will not call the
ActionForm's validate method. Then in your Action's execute method call the
ActionForm's validation method yourself.

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


   if ("Submit".equals(form.getMyParameter())) {

       ActionErrors errors = form.validate(mapping, request);
       saveErrors(request, errors);

       if (errors == null || errors.isEmpty()) {
           // do my DAO stuff here
          return mapping.getInputForward();
       }

   }

}

Niall

----- Original Message ----- 
From: "kimbuba" <ki...@yahoo.it>
To: <us...@struts.apache.org>
Sent: Wednesday, May 12, 2004 12:26 AM
Subject: Re: ValidatorForm exception handling


> Thnx Niall but i don't understand how to set up that.
>
> Here's my situation
> i got one action MyAction.
> and MyForm.
>
> i have a very basic validation
> MyAction if has parameter="Submit" it will do prevalidation stuff like
> loading beans to populate fileds otherwise it will let ActionForm validate
> and execute business logic.
> 1. go to MyAction and execute pre validation stuff, like access a bean
from
> DB get the ActionForm form signature execute method and set it with bean
> values.
> 2. go to myLayout don't validate and show my form with all fields
populated.
> 3.SUBMIT --> myPathSubmit --> go back to MyAction that will dispatch to
> submit logic, but before do that, go to MyForm bean and:
> 4. check validation basic stuff (like required, filed length..) and
>     4.a --> on success go to success
>     4.b --> on validation errors go back to check validation.
>     until now it is a very common validation but when my ActionForm will
> show errors i
>     would like to put on my request some other info, that came from DAO
> resources but than could throw some Exceptions.
>
> where do i have to put my own Action's execute validation dao logic plus
> basic automatic validation suffs?
>
> here's my mappings:
> <action type="MyAction"
> path="/myPath"
> input=".myLayout"
> name="MyForm"
> scope="request"
> validate="false">
> <forward name="success" path=".mySuccessLayout" />
> </action>
>
> <action type="MyAction"
> path="/myPathSubmit"
> parameter="Submit"
> input=".myLayout"
> name="MyForm"
> scope="request"
> validate="true">
> <forward name="success" path="/mySuccessLayout2.do" />
> </action>
>
> Thnx
>
> "Niall Pemberton" <ni...@blueyonder.co.uk> ha scritto nel
> messaggio news:029801c4379a$14faab80$c1a92b52@DELL1800...
> > Do that kind of validation in your Action rather than ActionForm, that
way
> > you can use the struts exception handling.
> >
> > In the Action's execute method....
> >
> > if (errors from Business Delegate) {
> >   ActionErrors errors = new ActionErrors();
> >   errors.Add(....);
> >   saveErrors(request, errors);
> >   return mapping.getInputForward();
> > }
> >
> > Niall
> >
> >
>
>
>
>
> ---------------------------------------------------------------------
> 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: ValidatorForm exception handling

Posted by kimbuba <ki...@yahoo.it>.
Thnx Niall but i don't understand how to set up that.

Here's my situation
i got one action MyAction.
and MyForm.

i have a very basic validation
MyAction if has parameter="Submit" it will do prevalidation stuff like
loading beans to populate fileds otherwise it will let ActionForm validate
and execute business logic.
1. go to MyAction and execute pre validation stuff, like access a bean from
DB get the ActionForm form signature execute method and set it with bean
values.
2. go to myLayout don't validate and show my form with all fields populated.
3.SUBMIT --> myPathSubmit --> go back to MyAction that will dispatch to
submit logic, but before do that, go to MyForm bean and:
4. check validation basic stuff (like required, filed length..) and
    4.a --> on success go to success
    4.b --> on validation errors go back to check validation.
    until now it is a very common validation but when my ActionForm will
show errors i
    would like to put on my request some other info, that came from DAO
resources but than could throw some Exceptions.

where do i have to put my own Action's execute validation dao logic plus
basic automatic validation suffs?

here's my mappings:
<action type="MyAction"
path="/myPath"
input=".myLayout"
name="MyForm"
scope="request"
validate="false">
<forward name="success" path=".mySuccessLayout" />
</action>

<action type="MyAction"
path="/myPathSubmit"
parameter="Submit"
input=".myLayout"
name="MyForm"
scope="request"
validate="true">
<forward name="success" path="/mySuccessLayout2.do" />
</action>

Thnx

"Niall Pemberton" <ni...@blueyonder.co.uk> ha scritto nel
messaggio news:029801c4379a$14faab80$c1a92b52@DELL1800...
> Do that kind of validation in your Action rather than ActionForm, that way
> you can use the struts exception handling.
>
> In the Action's execute method....
>
> if (errors from Business Delegate) {
>   ActionErrors errors = new ActionErrors();
>   errors.Add(....);
>   saveErrors(request, errors);
>   return mapping.getInputForward();
> }
>
> Niall
>
>




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


Re: ValidatorForm exception handling

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Do that kind of validation in your Action rather than ActionForm, that way
you can use the struts exception handling.

In the Action's execute method....

if (errors from Business Delegate) {
  ActionErrors errors = new ActionErrors();
  errors.Add(....);
  saveErrors(request, errors);
  return mapping.getInputForward();
}

Niall


----- Original Message ----- 
From: "kimbuba" <ki...@yahoo.it>
To: <us...@struts.apache.org>
Sent: Tuesday, May 11, 2004 9:39 PM
Subject: ValidatorForm exception handling


> Hello!
>
> Here is my problem:
>
> On ActionForm validate method i have a BusinessDelagate who access some
DAO
> resources.
>
> Because of that, probably my businessdelegate will throw some kind of
> exceptions i.e. MyDAOException.
> Is there any kind of solution to handle such exceptions?
>
> On Actions i can catch my exceptions to appropriate global-exception
> handlers.
>
> Because  validate(ActionMapping mapping,HttpServletRequest request)
doesn't
> throw any exception i can't
> go with an overwritten validate with an exception to be thrown.
> If i try with a RuntimeException it will break with an "internal error"
> (tomcat)
>
> Any ideas?
> Thnax!
>
>
>
>
> ---------------------------------------------------------------------
> 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