File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Should I use form beans ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Should I use form beans ?" Watch "Should I use form beans ?" New topic
Author

Should I use form beans ?

Atul Mishra
Ranch Hand

Joined: Jun 08, 2006
Posts: 140
All,

I use jsp pages, struts action classes, struts config files and the whole package. But I am not using form beans. Should I ?

What are the advantages ?

Thanks
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

http://www.technicalfacilitation.com/examscam/get.php?link=../irad/tutorials

Form beans can capture and validate incoming parameters from the client. If the client has given you garbage, you can redirect the user to an error page, or ask for more elaborate input, and not bother you're very busy Struts action with verboten data.

Plus, Form objects slide right into a memento pattern, where everything a user typed into a long form is redisplayed, so the user doesn't have to fill out an 300 field form, when only one field was entered incorrectly.

Here's a link that provides a multimedia flash presentation on how you can use Struts forms, and how they can be used for input validation/memento pattern. It's a great little tutorial.

Look for the "Fine Form" link:

http://www.technicalfacilitation.com/examscam/get.php?link=../irad/tutorials
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you don't use ActionForm beans, you're only getting about half the benefits of using Struts. They provide a Java representation of the data that is input in your HTML forms that is populated automatically by the Struts Framework. They allow you to use the <html:xxx> input tags that save you a lot of work by automatically displaying a value if one exists for the ActionForm bean property specified.

Another benefit is that they make the Java code in your action class cleaner and less error prone.

for example:

String userName = myFormBean.getUserName();

is much cleaner than

String userName = request.getParameter("userName");

For one thing, if I misspell getUserName() the compiler will correct me. If I misspell "userName", I won't find out until run time.


Merrill
Consultant, Sima Solutions
Atul Mishra
Ranch Hand

Joined: Jun 08, 2006
Posts: 140
Thnaks for the replies.

Regarding using tags like <html:text>, I have to display values for these form objects dynamically. Thats the main reason why I stayed away from using <html:text> like tags. Basically I cannot have nested tags and then I have to use scriptlets to display the value.

But form beans, I think I have to start incorporating in my view.

Right now I have my JSP pages and Action classes. In this, how do I add a layer of form beans ? I use eclipse. Will it support an addition ?

Thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Regarding using tags like <html:text>, I have to display values for these form objects dynamically. Thats the main reason why I stayed away from using <html:text> like tags. Basically I cannot have nested tags and then I have to use scriptlets to display the value.


This reason for not using <html:text> tags does not make any sense to me. I use <html:text> tags precisely because I want to display the values dynamically. If you really don't want to use them, that's fine, but I think you'd do well to find out what they really do before dismissing them.
how do I add a layer of form beans ?

The best way to learn this process is to do a Struts tutorial. Here is a good one that uses eclipse. The tutorial assumes you have the My Eclipse plugin. It is a commercial plugin, but it only costs about $30 US per year, and you can get a 30 day free trial.

This link has a number of other tutorials.

Once you've worked through one of these, I think you'll understand how the whoe form bean thing works. If you still have questions, let us know.
Atul Mishra
Ranch Hand

Joined: Jun 08, 2006
Posts: 140
Thanks Merill.

On the process..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Should I use form beans ?