3:41 am - Saturday August 19, 2017

Struts 1x html RadioButton tutorial example program

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

This Struts Tutorial explains how to create Radio button for your struts application using struts-html.tld file. Radio button is used for choosing either or option, we can select only one option at a time.


Project Structure:

Jar Files: 

TLD Files: 

Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from 

JAR FILES + TLD FILES

LoginAction.java: 

package Com.Candidjava;

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 LoginAction extends Action {

	private final static String SUCCESS = "success";

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		LoginForm loginForm = (LoginForm) form;

		return mapping.findForward(SUCCESS);

	}

}

LoginForm.java:

package Com.Candidjava;

import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm {

	private String gender;

	public String getGender() {

		return gender;
	}

	public void setGender(String gender) {

		this.gender = gender;
	}
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<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>
		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<jsp-config>
		<taglib>
			<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
		</taglib>
		<taglib>
			<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
			<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
		</taglib>
	</jsp-config>
</web-app>

struts-config.xml:


<?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="LoginForm" type="Com.Candidjava.LoginForm" />

	</form-beans>

	<global-exceptions>

	</global-exceptions>

	<global-forwards>
		<forward name="welcome" path="/Welcome.do" />
	</global-forwards>

	<action-mappings>
		<action input="/index.jsp" name="LoginForm" path="/Login"
			scope="session" type="Com.Candidjava.LoginAction">
			<forward name="success" path="/success.jsp" />
			<forward name="failure" path="/failure.jsp" />
		</action>
		<action path="/Welcome" forward="/welcomeStruts.jsp" />
	</action-mappings>

	<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor" />

	<message-resources parameter="Com/Candidjava/ApplicationResource" />

	<plug-in className="org.apache.struts.tiles.TilesPlugin">
		<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
		<set-property property="moduleAware" value="true" />
	</plug-in>

	<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
		<set-property property="pathnames"
			value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
	</plug-in>

</struts-config>

index.jsp:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<center><html:form action="/Login">

	<table bgcolor="pink" align="center">

		<tr>
			<td>Gender:</td>
		</tr>
		<tr>
			<td>Male<html:radio property="gender" value="male"></html:radio>
			</td>
		</tr>
		<tr>
			<td>Female<html:radio property="gender" value="female"></html:radio>
			</td>
		</tr>

		<tr>
			<td><html:submit value="submit" /></td>
		</tr>

	</table>

</html:form></center>

</body>
</html>

success.jsp:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>

<body bgcolor="		#CCCCFF  ">
<center><br />
<br />
<br />
<br />
<br />
<br />

<div style="color: red">
<h1>Gender<bean:write name="LoginForm" property="gender"></bean:write>
<br>
</div>
</center>
</body>

</html>

OUTPUT:

 

DOWNLOAD+SOURCE CODE

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply