Solved

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

Posted on 2006-06-27
7
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Multi-threading long-running processes can have a significant increase in overall performance and drastically decrease over time it takes for a process to complete. Unfortunately, not all applications support native multi-threading, some by design a…
This article outlines some of the reasons why an email message gets flagged as spam on a recipient's end.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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