Download this Struts radio option example –

In this Struts example, you will learn how to create a HTML radio option with Struts <html:radio> tag.

1. Folder Structure

This is the final project structure created by Maven. Please create the corresponding folders.


2. Action class

Create an Action class, do nothing but forward the request.

package com.mkyong.common.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.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.mkyong.common.form.HtmlRadioForm;
public class HtmlRadioAction extends Action{
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
		HtmlRadioForm htmlRadioForm = (HtmlRadioForm)form;
		return mapping.findForward("success");

3. Properties file

Create a properties file, and declare the error and label messages.

#error message = Please select a radio option.
#label message = Select Sex = Male = Female = Submit = Reset

4. ActionForm

Create a ActionForm, contains a sex variable for radio option and form validation – validate().

package com.mkyong.common.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class HtmlRadioForm extends ActionForm{
	String sex;
	public String getSex() {
		return sex;
	public void setSex(String sex) { = sex;
	public ActionErrors validate(ActionMapping mapping,
		HttpServletRequest request) {
	    ActionErrors errors = new ActionErrors();
	    if( getSex() == null || ("".equals(getSex())))
	    	  new ActionMessage(""));
	    return errors;
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// reset properties
		sex = "";

5. JSP Page

Use the Struts’s HTML tag <html:radio> to create a HTML radio option.


<%@taglib uri="" prefix="html"%>
<%@taglib uri="" prefix="bean"%>
<h1>Struts html:radio example</h1>
<html:form action="/Radio">
<html:messages id="err_name" property="">
<div style="color:red">
	<bean:write name="err_name" />
<div style="padding:16px">
<bean:message key="" /> :
<html:radio property="sex" value="male" /> 
<bean:message key="" />
<html:radio property="sex" value="female" /> 
<bean:message key="" />
<div style="padding:16px">
	<div style="float:left;padding-right:8px;">
                    <bean:message key="" />
           <bean:message key="" />

Get the radio option value from htmlRadioForm form and display it


<%@taglib uri="" prefix="bean"%>
	Your selected value is : 
	<bean:write name="htmlRadioForm" property="sex" />

6. struts-config.xml

Create a Struts configuration file and link all together.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
			<forward name="success" path="/pages/display.jsp"/>
		parameter="" />

7. web.xml

Final step, create a web.xml and integrate the Struts framework.

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >
  <display-name>Maven Struts Examples</display-name>

Access it



Select a sex and pressed the submit button, it will forward to


and display the select radio option.

Tags :
Founder of, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts