Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-06-27
7
Medium Priority
?
394 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 176 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 172 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 172 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Did you know there are services out there that can turn an Instagram feed into an RSS feed? I found some interesting exclusive Instagram content which I wanted to follow without signing up for yet another social media account. RSS to the rescue!
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

971 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