Solved

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

Posted on 2006-06-27
7
379 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A procedure for exporting installed hotfix details of remote computers using powershell
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now