Struts HTML CheckBox Tag ( )

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

<%@ taglib prefix="html" uri="" %>

< html:checkbox >

< html:checkbox > -renders a checkbox input should of type boolean and any value should correspond to one of the string which indicate true like “true”,”yes” or “on”.

Example Code for < html:checkbox >

1.Create an Jsp page and name it as checkbox.jsp.It is the Welcome page for a user.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="" %>
<%@taglib prefix="bean" uri="" %>

< html>
    < head>

        < title>HTML CheckBox example </title>
    < head>
    < body>
        < h1 > Struts html:checkbox example </h1>

        < html:form action="/htmlcheckbox">
            < h5 ><bean:write name="checkform" property="error" filter="false"/>
           < b> Check Box:</b> < html:checkbox property="checkvalue"/> < br>
            < html:submit value="Click"/>
            < html:reset/>

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm

package com.myapp.struts;
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 checkform extends org.apache.struts.action.ActionForm {

    private String checkvalue;
    private String error;

    public String getError() {
        return error;

    public void setError() {
        this.error = "< span style='color:red'> Please check the check box</span>";

    public String getCheckvalue() {
        return checkvalue;

    public void setCheckvalue(String checkvalue) {
        this.checkvalue = checkvalue;

    public checkform() {


3.Simple Action class which is a sub class of Action class used to process the user’s request.

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class checkaction extends org.apache.struts.action.Action {

    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        checkform formBean = (checkform)form;
String val = formBean.getCheckvalue();
        return mapping.findForward(SUCCESS);
return mapping.findForward(FAILURE);

4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

< struts-config>
    < form-beans>
        < form-bean name="checkform" type="com.myapp.struts.checkform"/>

< action-mappings>
        < action name="checkform" path="/htmlcheckbox" scope="request" type="com.myapp.struts.checkaction" validate="false">
        < forward name="success" path="/checkboxoutput.jsp"/>
        < forward name="failure" path="/checkbox.jsp"/>


5.Create another simple Jsp page checkboxoutput.jspwhich is for displaying the output if the check box is checked else return error message.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="" prefix="bean" %>

< html>
    < head>
        < meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        < title> CheckBox OutPut page</title>
    < body>
        < h3> Check value is: < bean:write name="checkform" property="checkvalue"/></h3>

6.Building and running the application

Access page:http://localhost:8080/checkbox/

also read:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!