< html:radio /> html tag

< html:radio /> html tag

Previous Home Next

 

It render to Html input element with type radio.Radio button are used when you want to let the visitor select one option from a set of alternatives.
Example:-

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

<html:form action="/Radio" >
User Name :<html:text name="LoginForm" property="username" />
<html:radio property="sex" value="male" />
<html:radio property="sex" value="female"/>
<html:textarea property="address" cols="50" rows="50" />  
<html:submit value="submit"/>
</html:form>

There are the following attributes which is mostly used:-
  • property*  :- This is used to identified a data member of the bean associated with the input element.
  • value* :- it specifies the value of the input element.This is required attributes.
  • accesskey :-This attributes is used to immediately move focus to this element.
  • alt :- This attributes is the alternate text for this element 
  • onfocus :- This is used receives input focus after that event handler executed 
  • onmousedown :- This attributes is under the mouse pointer and a mouse button is depressed than event handler executed .
  • onmousemove :- This attributes is used under the mouse pointer when the pointer is moved than event handler executed .

 
The Following are many other attributes which we can use :-
  • altKey
  • bundle 
  • disabled
  • indexed 
  • onblur 
  • onchange
  • onclick 
  • ondblclick
  • onkeydown
  • onkeypress 
  • onkeyup
  • onmouseout
  • onmouseover
  • onmouseup
  • style
  • styleClass
  • tabindex
  • title
  • titleKey
  • errorKey
  • errorStyle
  • errorStyleId
  • idname

Directory Structure of RadioTagExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Struts html:radio tag example</title>
</head>
<body>
<h3>Struts html:radio tag example</h3>
<html:form action="/radioAction">
<bean:message key="label.name"/>:
<html:text property="name" name="radioForm"/><br/><br/>
<bean:message key="label.gender"/>:<bean:message key="label.male"/>
<html:radio property="gender" value="male" /><bean:message key="label.female"/>
<html:radio property="gender" value="female"/><br/><br/>
<html:submit><bean:message key="label.submit" /></html:submit>
<html:reset><bean:message key="label.reset" /></html:reset>
</html:form>
</body>
</html>

web.xml

<!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>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans>
    <form-bean name="radioForm" type="org.r4r.struts.RadioForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action path="/radio" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/>
		<action path="/radioAction" type="org.r4r.struts.RadioAction" name="radioForm" input="/index.jsp">	
         <forward name="success" path="/success.jsp"/>
		</action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

RadioForm.java

package org.r4r.struts;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class RadioForm extends ActionForm {
	private String name;
	private String gender;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
}

RadioAction.java

package org.r4r.struts;

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;

public class RadioAction extends Action {
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) throws Exception{
		@SuppressWarnings("unused")
		RadioForm passwordForm=(RadioForm)form;
		return mapping.findForward("success");
	}
}

ApplicationResources.properties

# Resources for parameter 'org.r4r.struts.ApplicationResources'
# Project Struts1.3_RadioTagExample
#label message
label.name = User Name 
label.gender = Gender 
label.male = Male
label.female = Female
label.submit = Submit
label.reset = Reset

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
<title>Struts html:radio tag example</title>
</head>
<body>
<h3>Struts html:radio tag Example</h3>
<h4>Welcome,<bean:write name="radioForm" property="name" /></h4>
<h4>Your gender is,<bean:write name="radioForm" property="gender" /></h4>
</body>
</html>

Output




Previous Home Next