Services | Updates | Contact
Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
Search All Tutorials
Top Search: Loan Struts Open Source
What is Spyware and Adware?
After the burst of Boradband Internet, it have become very easy to download the software from Internet. Usually many Int
 
Boolean Expressions
Boolean Expressions
 
More Tutorials...

Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

 
 
Struts
Comments
 
 

 

Checkbox Tag <html:checkbox>:

                         

html: checkbox Tag -  is used to create a  Checkbox Input Field.

This tag creates an HTML <input> element of type checkbox, populated from the specified value or the specified property of the bean  associated with our current form. 

  NOTE :

  1. This tag is only valid when nested inside a form tag body.
  2. The property value associated with this field should be of type Boolean.
  3. Any value you specify should correspond to one of the Strings that indicate a true value ("true", "yes", or "on").
  4. The ActionForm bean associated with this form must include a statement setting the corresponding Boolean 
    property to false in the reset() method. It avoid error to correctly recognize unchecked checkboxes. 
Name Description
disabled Set to true if this input field should be disabled.
name The attribute name of the bean whose properties are consulted when rendering the current value of this input field. If not specified, the bean associated with the form tag we are nested within is utilized.
property Name of the request parameter that will be included with this submission, set to the specified value.
value The value to be transmitted if this checkbox is checked when the form is submitted. If not specified, the value "on" will be returned.

Example Illustrating the use of the Radio<html:checkbox > tag :
Here you will learn to use the Struts Html <html:checkbox > tag. 
We will cover an example that will show a working of<html:checkbox > tag.

Example code :
Creating an Action Class :
Develop a simple action class CheckBoxTagAction.java.


package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class CheckBoxTagAction extends Action {
    private final static String SUCCESS = "success";
  
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        return mapping.findForward(SUCCESS);
        
    }
}

Creating Form Bean :
Develop a simple Form Bean class CheckBoxTagActionForm.java
package ActionForm;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class CheckBoxTagActionForm extends 

org.apache.struts.action.ActionForm {
    
    private String select;
    
    public String getSelect() {
        return select;
    }
    
    public void setSelect(String string) {
        select= string;
    }
    
    public CheckBoxTagActionForm() {
        super();
    
    
}


Defining form Bean in struts-config.xml file :

Add the following entry in the struts-config.xml file for defining the form bean :
<form-bean name="CheckBoxTagActionForm" 
     
type="ActionForm.CheckBoxTagActionForm"/>

Developing the Action Mapping in the struts-config.xml :
Here, Action mapping helps to select the  Action, Action Form and other configuration related 
information  for specific requests.

<action input="/" name="CheckBoxTagActionForm" path="/CheckBoxTagAction" 
   scope="request" type="action.CheckBoxTagAction" validate="false">

<forward name="success" path="/HtmlCheckBoxTagOutPut.jsp"/>

</action>

Developing the HtmlCheckBoxTag.jsp page :
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body bgcolor="#999933">
    <h3><font color="#FFFF33"> select CheackBox For Demonstration.........
        </font>
   </h3>

        <table border="2">
            <tr>
                <th align="right"><font color="#FFFF33">CheckBoxTag Demo:
                                  </font>
               </th>

                
                <html:form action="CheckBoxTagAction" method="POST">
                    <td align="left">select 
             <html:checkbox name=
"CheckBoxTagActionForm" property="select" />
                    </td>
                    <td align="left">
                        <html:submit/>
                    </td>
                </html:form>                
            </tr>
        </table>        
    </body>
</html>


Developing the HtmlCheckBoxTagOutPut.jsp page :
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><font color="#FFFF33">OUT PUT</font></title>
    </head>
     <body bgcolor="#999933">  
        <h3><font color="#FFFF33">OUT PUT</font></h3>   
        <h3><font color="#FFFF33">Selected Value is.......</font></h3>
        <bean:write name="CheckBoxTagActionForm" property="select"/>     
    </body>
</html>
 

Add the following line in the index.jsp to call the form :
<a href="HtmlCheckBoxTag.jsp">HtmlCheckBoxTagDemo</a><br/>

Building and Testing the Example :
Build and deploy and Test  the application . 
Open the browser and navigate to the HtmlCheckBoxTag.jsp page. 
Your browser displays the following page.


Don't select  checkbox on the HtmlCheckBoxTag.jsp  page, and see the output.

Output:

Now select the checkbox on  the HtmlCheckBoxTag.jsp, and see the output.

Output:

Above demo  Displays the working of<html:checkbox>tag.

                         
Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

Its works fine for submitting the data from UI to server...

suppose if the data comes from the server.. how will you make sure that it will work..

what I mean is.. suppose if a response is coming from server how will you show that box is selected with this approach...??

Posted by anil on Thursday, 01.10.08 @ 19:12pm | #44908

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.