Tutorials : Advanced Forms Handling in Struts 1.1 :

Advanced Forms Handling in Struts 1.1

by Keld H. Hansen

Introduction

This article is about the more advanced features Jakarta Struts offers in building HTML forms. If you know how to create forms in plain HTML then the step to building simple forms in Struts with, for example, a couple of input text fields, a checkbox or a radio button is not very complicated. When it comes to the more complex controls like the multi-valued selection list or a variable length list of input fields it gets more challenging. This is especially true when the possible selections are not fixed, but taken from some external source like a database instead. I've too often found myself struggling with the syntax and semantics of the HTML-tags when the forms get complex, and if you search the web for advice, you'll soon see that you have to collect information from many sources.

In this article I've tried to collect solutions to the most common non-trivial cases. Every case, of course, is not covered here. Remember to also read Struts' own documentation, starting with the documentation for the HTML-tags. You might find what you need there.

At the end of the article is a table with an overview of the solutions. I'm sure you'll find it useful when coding your Struts applications.

Here's a link to a zip file containing all the examples shown in the article.

The controls in a form

Here is a list of the most commonly used input controls:

Type Look Values
1. Text field Anything that can be typed in.
One line
2. Text area field Anything that can be typed in.
Several lines.
3. Checkbox 1 of 2 (yes/no - on/off)
4. Radio button 1 of many fixed values
5. Selection list -
single type
1 of many fixed values.
Gives you the same functionality as the radio buttons, only the presentation differs.
 
6. Selection list -
multiple type
Several of many fixed values
7. List of checkboxes Several of many fixed values.
Gives you the same functionality as the multi selection list, only the presentation differs.  

In Struts you define a control with a tag from Struts' HTML- library. The next table shows how these are mapped to the old, well-known HTML tags:

Type Struts html-tag HTML tag
1. Text field <html:text property="firstname"/> <input type="text" name="firstname">
2. Text area field <html:textarea property="address"/> <textarea name="address">
3. Checkbox <html:checkbox property="married" value="yes"/> <input type="checkbox" name="married" value="yes">
4. Radio button <html:radio property="card"
value="Diners"/> . . .
<input type="radio" name="card" value="Diners"> . . .
5. Selection list -
single type
<html:select property="country"> and
<html:option value="F"/> . . .
<select name="country">
<option value="F"> . . .
6. Selection list -
multiple type
<html:select property="food" multiple="true"> and
<html:option value="milk"/> . . .
<select name="food" multiple>
<option value="milk"> . . .
7. List of checkboxes <html:multibox property="site">
. . .
<input type="checkbox" name="site" value="jb">
<input type="checkbox" name="site" value="oj">
. . .

How to Add Java Applets to Your Site

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.