Download this Struts radio option example – Struts-Radio-Example.zip

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.

Struts-radio-folder

2. Action class

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

HtmlRadioAction.java


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.

Common.properties


#error message
error.common.html.radio.required = Please select a radio option.

#label message
label.common.html.radio.name = Select Sex
label.common.html.radio.sex.male = Male
label.common.html.radio.sex.female = Female
label.common.html.radio.button.submit = Submit
label.common.html.radio.button.reset = Reset

4. ActionForm

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

HtmlRadioForm.java


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) {
		this.sex = sex;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
		HttpServletRequest request) {
		
	    ActionErrors errors = new ActionErrors();
	      
	    if( getSex() == null || ("".equals(getSex())))
	    {
	       errors.add("common.radio.err",
	    	  new ActionMessage("error.common.html.radio.required"));
	    }
	      
	    return errors;
	}
	
	@Override
	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.

radio.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>





Struts html:radio example

:

Get the radio option value from htmlRadioForm form and display it

display.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>





Your selected value is :

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" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">

<struts-config>

	<form-beans>
		<form-bean
			name="htmlRadioForm"
			type="com.mkyong.common.form.HtmlRadioForm"/>
	
	</form-beans>

	<action-mappings>
	
	        <action
			path="/RadioPage"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/pages/radio.jsp"/>
	
		<action
			path="/Radio"
			type="com.mkyong.common.action.HtmlRadioAction"
			name="htmlRadioForm"
			validate="true"
			input="/pages/radio.jsp"
			>	
			
			<forward name="success" path="/pages/display.jsp"/>
		</action>
	</action-mappings>

	<message-resources
		parameter="com.mkyong.common.properties.Common" />

</struts-config>

7. web.xml

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


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Maven Struts Examples</display-name>
  
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
         /WEB-INF/struts-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
    
  <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping>

</web-app>

Access it

http://localhost:8080/StrutsExample/RadioPage.do

Struts-radio-example1

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

http://localhost:8080/StrutsExample/Radio.do

and display the select radio option.