Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Struts Drop Down Box Example

In this article we will how do we handle drop-down lists into Struts Web Application and also how do we render response based on Drop-down list.

Struts a web application framework also provides support for drop-down lists. Drop-down lists is a html attribute that holds multiple values. Out of these values, a single value is selected that need to be handled by struts, while an html form is submitted. This article will let you go with an example that will process drop-down list values selected and will process result according to these values.

Uploading a file using Struts web application in detail:

Struts provide a separate way to create drop-down lists and to access their values. We can create a drop-down list in two ways:

  • Using Struts Tags in JSP
  • Using Struts Action class

We will learn both techniques into this article. To initialize a drop-down list using Action class, we need to create a class member variable into the action class that will be passed into the struts tag <s:select> to its attribute “list”.

This class member variable has a setter() methods to hold its value. A setter() method may be initialized using collection list.

To access the value of this kind of list, we need to create one more class member variable and define this value into the attribute value “value”.

The other way to initialize a drop-down list is by defining a list into the struts tag. There is a specific syntax to create this list as follow:

#{‘key1’:’value1’, ‘key2’:’value2’,….}

To access the value of this list, we say:

#{‘key’}

To create a struts web application, we are required to have following tools and applications:

  • Maven (Maven 3)
  • Eclipse IDE (Eclipse Juno)
  • JDK Run time environment (JDK1.7)
  • Application or web server (Tomcat 7)
  • Struts 2 Library pulled by maven

A directory structure created by eclipse ide for the dynamic web application is shown into the figure that also list struts 2 jar file downloaded from maven repository to run this struts web application as shown in Figure 1:

Directory structure created by Eclipse IDE for Struts 2 Web Application

Figure 1: Directory structure created by Eclipse IDE for Struts 2 Web Application

Figure 1 shows the struts 2 web application directory structure created using Maven repository into Eclipse IDE. The red rectangle highlight Struts 2 jar file required to run this web application.

So, let us go with an example listed below. Listing 1 defines select.jsp files that create an interface to user as:

Listing 1: select.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>Struts 2 Drop-down App</h1>

<s:form action="resultAction" namespace="/">

<h4>
	<s:select label="Choose your name from drop down"
	 headerKey="-1" headerValue="Select Name" 
	 list="#{'Umesh':'Umesh', 'Deepak':'Deepak', 'Dipu':'Dipu'}" name="yourName" value = "#{'Umesh'}"/>
<s:select label="What's your city" 
		headerKey="-1" headerValue="Select City"
		list="city" 
		name="yourCity" 
		value="defaultCity" />
</h4> 
<s:submit value="submit" name="submit" />
	
</s:form>

</body>
</html>

Listing 1 defines select.jsp file that create two drop-down lists. One is created by defining a list into jsp file and second is created using action class, where “city” is a class member variable that is used to access the list elements, “defaultCity” is used to access the default value of the list from the action class.

Now let us define an action class “SelectAction.java” that handles these drop-down lists and process their contents to result.jsp file as listed:

Listing 2: SelectAction.java

package com.dropdown.action;

import java.util.ArrayList;
import java.util.List;

public class SelectAction{

	private List<String> city;
	private String defaultCity;
	private String yourCity;
	private String yourName;
	
	public String getYourName() {
		return yourName;
	}

	public void setYourName(String yourName) {
		this.yourName = yourName;
	}

	public List<String> getCity() {
		return city;
	}

	public void setCity(List<String> city) {
		this.city = city;
	}

	public String getYourCity() {
		return yourCity;
	}

	public void setYourCity(String yourCity) {
		this.yourCity = yourCity;
	}

	public String getDefaultCity() {
		return "Agra";
	}
	
	public SelectAction(){
		city = new ArrayList<String>();
		city.add("Noida");
		city.add("Ghaziabad");
		city.add("Agra");
		city.add("Aligarh");
	}

	public String execute() {
		return "success";
	}
	
	public String display() {
		return "none";
	}	
}

So, Listing 2 defines an action class for drop-down list application and accepts values submitted from “select.jsp” file and initialize “city” drop-down list also. It defines four class member variables “city” (a collection list to initialize a city drop-down list), “defaultCity” (a variable used to initialize city drop-down list as its default value), “yourName” (get values submitted from “select.jsp” file and process it to “result.jsp” file) and “yourCity” (get values submitted from “select.jsp” file and process it to “result.jsp” file). “city”, “yourCity” and “youName” are defined with their getter() and setter() methods, whereas “defaultCity” is define with its getter() method.

Now, Let us define result.jsp file into Listing 3 as:

Listing 3: result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>Struts 2 Drop-down App</h1>

<h4>
  Dear User (<s:property value="yourName"/>), Your city is : <s:property value="yourCity"/>
</h4> 
</body>
</html>

Listing 3 defines “result.jsp” file that access values of “yourName” and “yourCity” variables from “select.jsp” file using “SelctAction.java” java class. To access the values of “select.jsp” file attributes struts provides a tag <s:property value = “action-class-variable-name-matches-to-jsp-file”/>

Once action classes and jsp files are defined into the application, it is required to map their entries into a struts configuration file that will manage their communication, once deployed this application on the web server. Listing 4 defines a struts.xml file as:

Listing 4: struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
	<package name="default" namespace="/" extends="struts-default">
		<default-action-ref name="selectAction"></default-action-ref>
		<action name="selectAction" class="com.dropdown.action.SelectAction" method="display">
			<result name="none">pages/select.jsp</result>
		</action>
		
		<action name="resultAction" class="com.dropdown.action.SelectAction">
			<result name="success">pages/result.jsp</result>
		</action>
	</package>
</struts>

Listing 4 defines xml file that configures a struts application. It defines a default package using <package> that is a sub-tag of <struts> tag. It defines “namespace” to default as “/” with package name “default” and extends “struts-default”. The <package> tag also defines two actions using <action> tag. First action is “selectAction” that lands to “SelectAction” class defined into package name “com.dropdown.action” and calls the method “display” of this class and loads the jsp file “select.jsp” defines into folder “pages”. The next action named “resultAction” also lands to the same class “SelectAction” and execute method “execute()” of this class and loads the jsp file “result.jsp” defined into folder “pages”.

Every web application contains a deployment descriptor file “web.xml” that manages the execution of the web application usually. This file redirects all the request to struts file class “StrutsPrepareAndExecuteFilter” defined into package name “org.apache.struts2.dispatcher.ng.filter” as listed into Listing 5:

Listing 5: 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>Struts 2 Web Application</display-name>
  
  <filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

Listing 5 defines <web-app> tag as a root tag that holds <display-name> tag that defines the application name and <filter> tag that is used to define struts filter class. This filter class needs to be mapped to all landing urls for this application, so that all the requests can be managed using by “struts.xml” file.

To deploy this application from eclipse ide, right click on the project and choose “Run As -> Run on Server” that loads the default package “selectAction” defined into “struts.xml” file as shown in figure 2:

select.jsp file, loaded on startup

Figure 2: select.jsp file, loaded on startup

Figure 2 loads “select.jsp” file, when the application is deployed and “selectAction” action is called. It calls the action class method “display()”, when select.jsp file is being called and initializes “city” drop-down list. Once this html form is submitted by hitting “submit” button, it redirects the selected values to “result.jsp” file as shown in Figure 3:

result.jsp file displays selected values from select.jsp

Figure 3: result.jsp file displays selected values from select.jsp

Figure 3 shows the values selected from “yourName” and “yourCity” drop-down lists and these values are displayed into “result.jsp” file using “execute()” method of “SelectAction” class.

Conclusion:

In this article, we learn about drop-down list and the ways to create a drop-down list using Struts framework. We went through an example that demonstrates all possible ways to create a drop-down list into a web application.



Have good knowledge on Java, HTML, CSS and Android platform and is pursuing Masters in Computer Applications.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login