This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Handle Exceptions errors in Struts Form" Watch "Handle Exceptions errors in Struts Form" New topic

Handle Exceptions errors in Struts Form

JD Thompson

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();

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

Joined: Jun 29, 2006
Posts: 21

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!!!

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
I agree. Here's the link:
subject: Handle Exceptions errors in Struts Form
jQuery in Action, 3rd edition