• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

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

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
dodowoman
Asked:
dodowoman
3 Solutions
 
radarshCommented:
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
 
dodowomanAuthor Commented:
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
 
gotekareddyCommented:
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
 
jaggernatCommented:
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
 
jaggernatCommented:
did you try it out?

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now