4:42 am - Tuesday April 25, 2017

Sample struts program using Select, Option and Options tag in Struts-html.tld

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 tutorial explains how to create dropdown selection using struts-html.tld file in struts application.

 SOURCE CODE:

struts option buttion source code download

 

 

Folder srtucture

 

index.jsp

 

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ 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>Option &amp; Options Example</title>
</head>
<body>
<html:form action="/option">
	<h3>select Animal</h3>
	<html:select name="optionForm" property="animal">
		<html:option value="">select Animal</html:option>
		<html:options name="optionForm" property="animalList" />
	</html:select>
	<html:submit value="submit"></html:submit>
	<html:reset value="reset"></html:reset>
</html:form>

</body>
</html>

Success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>success</title>
</head>
<body>
<h3>U select following animal</h3>
<font size="5" face="arial" color="red">
 <bean:write name="optionForm" property="animal" /> </font>

</body>
</html>



fail.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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>fail</title>
</head>
<body>
<h3>your are not select any animal</h3>
</body>
</html>

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="optionForm" type="com.candid.option.OptionForm">
		</form-bean>
	</form-beans>
	<action-mappings>
		<action input="/option.jsp" name="optionForm" path="/option"
			scope="session" type="com.candid.option.OptionAction">
			<forward name="success" path="/success.jsp"></forward>
			<forward name="fail" path="/fail.jsp"></forward>
		</action>
	</action-mappings>
</struts-config>

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>option.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>

OptionForm.java

package com.candid.option;

import java.util.ArrayList;

public class OptionForm extends org.apache.struts.action.ActionForm {

	private static final long serialVersionUID = 1L;

	private String animal;

	ArrayList animalList = new ArrayList();

	public String getAnimal() {
		return animal;
	}

	public void setAnimal(String animal) {
		this.animal = animal;
	}

	public ArrayList getAnimalList() {
		ArrayList list = new ArrayList();
		list.add(0, "cat");
		list.add(1, "dog");
		list.add(2, "goat");
		list.add(3, "cow");
		return list;
	}

	public void setAnimalList(ArrayList animalList) {
		this.animalList = animalList;
	}

}
<span style="color: #000000;">




optionAction.java

package com.candid.option;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class OptionAction extends org.apache.struts.action.Action {
	private static final String SUCCESS = "success";
	private static final String FAIL = "fail";

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		OptionForm option = (OptionForm) form;
		if (option.getAnimal().equals("")) {

			return mapping.findForward(FAIL);
		}

		return mapping.findForward(SUCCESS);

	}

}


Output



output screenshot



output screenshot success page



output screenshot error page

DOWNLOAD + SOURCE CODE


Filed in: Z-A All Java Codes, Z-AStruts Codes

No comments yet.

Leave a Reply