Solved

new to struts: does ActionForm have to match all fields on JSP?

Posted on 2006-06-27
7
387 Views
Last Modified: 2010-04-01
Experts,

I have an elementary question about Struts: Does ONE JSP page always match to ONE ActionForm? I have a page which has Company info on the top, and customer info on the buttom. So, I created two Java Beans called Company and Customer to represent all the fields. But, in order to use ActionForm, should I use ONE actionForm with all the fields included on the JSP page? The reason for using ActionForm is I need to validate some fields before Action class after submit. So, I am not sure what I should do with my case. Could ActionForm has MORE or LESS fields than the JSP page?Should I even use ActionForm here?.thanks for help.
0
Comment
Question by:dodowoman
7 Comments
 
LVL 10

Accepted Solution

by:
radarsh earned 44 total points
ID: 16998737
Hi dodowoman,

Both Yes and No.

Yes if you use <html:xxxx> tags such as input, radio, checkbox and many more.
No if you have <html:xxxx> tags such as button, submit, img, image etc.

You need to read the struts manual on their site and check out for which tags,
a property attribute is mandatory and if it is the case, does having a field
in the form become necessary.


________
radarsh
0
 

Author Comment

by:dodowoman
ID: 17000986
thanks, how about in terms of my first part of question? How to handle the different entities on the same page, like company and customer info? Do you use ONE actionForm for both?It does not fit the Object Orientated design, right?
0
 

Assisted Solution

by:gotekareddy
gotekareddy earned 43 total points
ID: 17029805
Hi dodowoman,

Actually ActionForms are used to carry the user entered data from the jsp page to the Action class.

So irrespective of how many jsps u r using in the presentation tire , u can always go for a single ActionForm defining all the fields with setXXX and getXXx methods.

Hope this clarifies ur doubt.


0
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 43 total points
ID: 17349624
Yes , you can put all information in one Actionform


>>>>>Should I even use ActionForm here

If i were you, i would Use DynaValidatorForms and not ActionForms.
You can use DynaValidatorForms in your struts-config file and you dont even have to create seperate java classes
like you do for ActionForms. Cool haa..

so in your struts config you would write

<form-bean name="CustomerInformationBean" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="name" type="java.lang.String" />
<form-property name="age" type="java.lang.int" />
<form-property name="customerChildren" type="java.util.ArrayList" />  //you can even assign a collection like arraylist

....
</form-bean>


Secondly, since you want to do validation, DynaValidatorForms support Struts Validation Framework.
Below is an example code

"Struts validation framework" is an open source technology to validate the fields in jsp.. Its very simple to use and very powerful.

Struts validation framework comprises primarily of two files, validation.xml and validation-rules.xml(provided by struts framework).

lets say you want to validate a field "xyz" in your jsp,
In your validation.xml file , you have to write

<form name="nameoftheformbean">
<field property="xyz" depends="required">
<arg0 key="showerrormessage"/>
</field>
</form>


<field property="xyz" depends="required">  //reuired is a rule defined in validation-rules.xml file which means that the field "xyz" cannot be null.
<arg0 key="showerrormessage"/>  //showerrormessage is defined in ApplicationResources.properties file like
showerrormessage=Please enter a value in the field xyz

and thats it . Its that simple.


In your struts-config , you have to put
validate="true" and
input="NameOfJSP.jsp"

Basically this means that perform the validation and if there are any errors show them on NameOfJSP.jsp

Like "required" is a rule, there are other rules.

Take a look at this excellent article from Orielley

http://www.onjava.com/pub/a/onjava/2002/12/11/jakartastruts.html

hope that helps
J
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17422252
did you try it out?

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
getting Http 404 error in jsp forms 8 81
parse example 13 95
How to set default webapp for host 6 47
print map entry 34 72
If you are looking at this article, you have most likely been hit by some version of ransomware and are trying to find out if there is anything you can do, or what way you should react - READ ON!
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question