Using Checkbox & Radio Tags, html:select, html:options in Struts Forms

By: Kamini Emailed: 1671 times Printed: 2153 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Checkbox and Radio tags

a. Add a checkbox to the CustomerForm.jsp. It will check if the customer would like to receive email notifications. Its property name is “recieveEmail” and is defined in ActionForm in the next step.
<html:checkbox property="receiveEmail"/>

b. Add a boolean field to CustomerForm called “recieveEmail”. Also add getters and setters. (In Struts, single checkboxes are always assigned to boolean fields in ActionForm).

c. Initialize the Boolean field to true in CustomerForm constructor. This will show the checkbox as checked during display.

d. Add three radio buttons to CustomerForm.jsp. At form submission, only the checked radio is submitted.
<html:radio property="preferredCarrier" value="UPS"/>UPS &nbsp;
<html:radio property="preferredCarrier" value="USP"/>USPS &nbsp;
<html:radio property="preferredCarrier" value="FDX"/>FedEx &nbsp;

e. Also add the display labels to the Resource Bundle for the radio and checkbox field

f. Add a string field called preferredCarried to CustomerForm along with getters and setters.

g. In the CustomerForm constructor, initialize the preferredCarrier field to “FDX”. See how the radiobox for FedEx gets checked in the html form.

Use html:select

a. Add a Select box with a option as follows:
<html:select property="address.state">
<html:option property=”NA”
labelProperty=”--Select One--” />
</html:select>

Use html:options tag

a. Create a states.properties file under src/java/struts.example. 

b. Add a options tag to display states as follows:
<html:select property="address.state">
<html:option property=”NA”
labelProperty=”--Select One--” />

<html:options collection=”STRUTS_EXAMPLE_STATES”
property=”value” labelProperty=”label” />

</html:select>

NOTE: The tag is now looking for a collection as a page/request/session/application scope with the name STRUTS_EXAMPLE_STATES. Various options exist to create the collection and put it in one of the scopes.

Create in the JSP page
Create in the ActionForm and put it in request
Create in the ActionForm and put in Session
Create somewhere else and put in Application scope

The final option is the best since everybody uses the same read only data. We use a ServletContextListener to create the states in the application scope.


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(4)


1. View Comment

I need a sample jsp(using Struts html tags) in three dropdown lists where upon select and clicking the 1st dropdownlist, it should display the 2nd dropdownlist relevant to 1st dropdown list and also upon selection and clicking of 2nd dropdown list it should display 3rd dropdown list relevant to 1st and 2nd dropdown list selection.

For example if "A" is selected it should display "A1","A2","A3" and further selection it should display "A100", "A200", "A300".

For example if "B" is selected it should display "B1","B2","B3" and further selection it should display "B100", "B200", "B300".


View Tutorial          By: Anil Kumar at 2008-10-23 04:14:51
2. View Comment

I have one doubt from drop down box, i have two value in drop down box.1 yes is 2 no,i save the value no in database.if i try to retrive the value in data base and show the value in drop down. the no value did not show the first value yes only show can you give solution

View Tutorial          By: subash at 2008-11-09 22:27:45
3. View Comment

i have using struts tag in jsp ,
i have use javascript on this page for
<html:radio property="preferredCarrier" value="UPS"/>
but i have not validate this field in java script
how to validate this field
plz give me information


View Tutorial          By: sachin khatode at 2008-11-11 01:24:33
4. View Comment

I need a sample example using checkbox in struts.the selected value of the checkbox wants to insert into the database.

View Tutorial          By: Nandhu at 2011-02-25 07:45:39

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Kamini
Importing the Certificate Reply from the CA example using keytool in Java
The switch Statement example in Java
Joins example in SQL
ERRNO.H Header File in C
XDoclet struts-config.xml in Struts
Macro Substitution using #define in C
Using Checkbox & Radio Tags, html:select, html:options in Struts Forms
Do while Loops in C
Functions in C
Opening files for read and write in C++
Operator Precedence in C++
StringItem sample program in J2ME
AWT-based applications with a User Interface Window in Java
store() and load() example in Java
Stray or Dangling Pointers in C++

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
ForwardAction in Struts
What is Struts? Which Version of Struts to use?
Creating Struts Modules
Protecting JSPs from direct access in Struts
Handling Duplicate Form Submissions in Struts
Using JavaScript to submit a form in Struts
Configuring JDBC DataSources in Struts
Downloading and installing Struts
Struts 1 vs Struts 2
Introduction to Struts Architecture
Installing and configuring Tomcat and Struts
Using Multiple Message Resource Bundles in Struts
Using Checkbox & Radio Tags, html:select, html:options in Struts Forms
Use ImageButtonBean for Image based Form Submits in Struts
Most Emailed Articles (in Struts)
What is Struts? Which Version of Struts to use?
Is Struts the most popular web application framework for Java?
What is a Model View Controller (MVC) Model?
WebWork 2 is now Apache Struts 2
MVC Architecture (Model 2 Architecture)
Struts Classes
Creating Struts Modules
Handling multiple buttons in HTML Form in Struts
Handling Duplicate Form Submissions in Struts
Guidelines for Struts Application Development
Chaining actions in Struts
History of Struts
Configuring JDBC DataSources in Struts
Downloading and installing Struts
Editing struts-config.xml in a Struts Application