|
To begin with, you'll notice that where Struts uses the attribute name
"property", HTML uses "name". This is a bit confusing since Struts also has an
attribute called "name", which is used to give the name of the bean whose
"property" maps the control. The default for the "name" attribute is the name of
the form's corresponding bean, so normally you don't need to use the
"name" attribute. Mapping and accessing the controls
Rule # 1: The name of every control (given by the "property"
attribute) must be defined in the
form's bean, which may be: 1. a Java ActionForm bean
defined in the form-beans section of the struts-config file, for example: <form-bean name="detailForm"
type="hansen.playground.DetailForm"/>
2. a "dynamic" form bean, also defined in the form-beans section
of struts-config, for example:
<form-bean
name="simpleForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="firstname" type="java.lang.String"/>
<form-property name="site" type="java.lang.String[]"/>
</form-bean>
Note that a control may be mapped to an array if it contains more than one
value.
Rule #2:
You may get or set the data in the controls from the
execute method in the Action class:
1. ActionForm bean:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DetailForm df = (DetailForm)form;
String index = df.getFirstName();
String[] site = df.getSite();
. . .
df.setFirstName("John");
. . .
You may of course also get or set the data in the ActionForm
itself - for example in the validate method.
2. Dynamic form bean:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaActionForm f = (DynaActionForm)form;
String firstName = (String)f.get("firstname");
String[] site = (String[])f.get("site");
. . .
f.set("firstname", "John");
. . .
The "interesting" controls
We'll not spend time on the simple controls "Text
field", "Text area field"" and
"Checkbox" (marked 1, 2, and 3 in the tables above)
since they're all "single-valued", and therefore
simple to handle in your classes. Instead we'll look further at
the controls that have a set of options or values attached.
Set of fixed options
Let's first take care of the simple situation where the possible options of these
controls are fixed. By "fixed" I mean that they won't change over time, for
example "Male/Female" or "Spring/Summer/Autumn/Winter". The jsp-code example for
such a control could be:
<html:radio property="sex" value="M"/>Male<br>
<html:radio property="sex" value="F"/>Female
Another example with a multi selection list:
<html:select property="food" multiple="true">
<html:option value="milk">Milk</html:option>
<html:option value="apple">Apple</html:option>
<html:option value="bread">Bread</html:option>
</html:select>
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|