aspose file tools*
The moose likes Struts and the fly likes Handle Exceptions errors in Struts Form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Handle Exceptions errors in Struts Form" Watch "Handle Exceptions errors in Struts Form" New topic
Author

Handle Exceptions errors in Struts Form

JD Thompson
Greenhorn

Joined: Jun 29, 2006
Posts: 21
I am having to get database information to initialize the Form values in the loadForm method. One such method throws an exception. How do I handle errors to be displayed when the form is being initialized?

[I recognize that having the Form throw an exception for the calling action class to catch is ONE answer, is there another way?]

Struts provides the ActionErrors/ActionMessages for the Action class, but what structure exists for the Form class of Struts?

Below is a VERY skeletal representation of what I am facing:

public class TestForm extends ActionForm
{
ArrayList states;

public TestForm loadForm()
{
states=new ArrayList();
states=someDBmethodCall();
}
}


New Developer<br />Happy to learn what I don't know<br />Happy to share what I do<br />
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Instead You could try keeping the Actionform 'dumb' and using a setup Action instead. You can have an ActionForm associated with multiple Actions, so basically, you create an action whose job is to populate the Form with the data you require.

By calling database code from your Actionforms, you're coupling your View very tightly to the data access code, which (in my opinion) is detail it needn't know about.

Does that help at all?
JD Thompson
Greenhorn

Joined: Jun 29, 2006
Posts: 21
Daniel,

Thanks tons for your input!! That is a great suggestion and I will pass it on to the rest of the project team.

Merry Christmas!!!

JD
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I agree with Daniel's suggestion. Your Action should be the bridge between your form and your data or business tier. I would not make database calls directly in a form.

- Brent
 
 
subject: Handle Exceptions errors in Struts Form