Struts HTML RadioButton Tag ( )

SHARE & COMMENT :

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="http://struts.apache.org/tags-html" 2="%&gt;

< html:radio >

< html:radio > -renders a radio button input field of HTML.To render a series of radio buttons idName attribute may be used to specify a selected radio button value.

Example Code for < html:radio >

1.Create an Jsp page and name it as radio.jsp.It is the Welcome page for a user.Radio button tag has the same property and “value” attribute should be different which makes the difference to select any one of them(Either Male or Female).

radio.jsp

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


< html>
    < head>

        < title> HTML RadioButton example </title>
    < head>
    < body bgcolor="DDDDDD">
        < h1 > Struts html:radio button example </h1>

        < html:form action="/htmlradiobutton">
            < h5 >< bean:write name="radioform" property="error" filter="false"/>
           < h4> Select The Gender< /h4>
            < table border="0">

                < tbody>
                    < tr>
                        < td> < b> Male< /b> < /td>
                        < td> < html:radio property="gender" value="Male"/> < /td>
                    < /tr>
                    < tr>
                        < td> < b> Female< /b> < /td>
                        < td> < html:radio property="gender" value="Female"/> < /td>
                    < /tr>
                < /tbody>
            < /table>

            < html:submit value="Click"/>
            < html:reset/>
        </html:form>
    </body>
</html>

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.In this example we had only one property “gender”.

radioform.java

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 radioform extends org.apache.struts.action.ActionForm {

    private String gender;
    private String error;

    public String getError() {
        return error;
    }

    public void setError() {
        this.error = "&lt; span style='color:red'&gt; Please select  the Gender&lt;/span&gt;";
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public radioform() {
        super();

    }
}

3.Simple Action class radioaction.java which is a sub class of Action class used to process the user’s request. The value attribute of the html radio tag is checked.
radioaction.java

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 radioaction 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 {
        radioform formBean = (radioform)form;
String val = formBean.getGender();
if("Male".equals(val) || "Female".equals(val))  //checking for selected value
        return mapping.findForward(SUCCESS);
formBean.setError();
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"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

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

< action-mappings>
        < action name="radioform" path="/htmlradiobutton" scope="request" type="com.myapp.struts.radioaction" validate="false">
        < forward name="success" path="/radiooutput.jsp"/>
        < forward name="failure" path="/radio.jsp"/>
            </action>
</action-mappings>

	</struts-config>

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

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

< html>
    < head>
        < meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        < title> Radio OutPut page</title>
    </head>
    < body bgcolor="DDDDDD">
        < h3> Selected value is: < bean:write name="radioform" property="gender"/></h3>
    </body>
</html>

6.Building and running the application
Output

Access page:http://localhost:8080/radio/

Struts Framework Articles

also read:

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better