Struts 2 <s:combobox> combo box example

Download It – Struts-ComboBox-Example.zip

In Struts 2 , the <s:combobox> tag is basically a drop down list grouped together with a single-line text box, allowing the user to either type a value directly into the text box or choose the value from the drop down list, and the selected value will populate to the text box automatically.

In case you are confuse between drop down list and combo box list, read the combo box definition from Wiki.

<s:combobox label="What's your favor fruit" 
		headerKey="-1" headerValue="--- Select ---"
		list="fruits" 
		name="yourFruits" />

Resulting the following HTML code…


<td class="tdLabel">
   <label for="resultAction_yourFruits" class="label">
       What's your favor fruit:
   </label>
</td> 
<td> 
<script type="text/javascript"> 
function autoPopulate_resultAction_yourFruits(targetElement) {
	if (targetElement.options[targetElement.selectedIndex].value == '-1') {
		return;
	}
	targetElement.form.elements['yourFruits'].value=
              targetElement.options[targetElement.selectedIndex].value;
}
</script> 
<input type="text" name="yourFruits" value="" id="resultAction_yourFruits"/>
<br /> 
<select onChange="autoPopulate_resultAction_yourFruits(this);"> 
    <option value="-1">--- Select ---</option> 
    <option value="Apple">Apple</option> 
    <option value="Banana">Banana</option> 
    <option value="Orange">Orange</option> 
    <option value="Watermelon">Watermelon</option> 
</select> 
</td> 	

The <s:combobox> tag will generate an input text box, drop down list with an “onChange()” behavior to call the generated JavaScript function to populate the selected value from the drop down list into the generated text box automatically.

To create a drop down list , you should use <s:select> tag instead.

Struts 2 <s:combobox> example

A full Struts 2 example to show the use of combo box via <s:combobox>

1. Action

Action class to generate and hold the selected combo box options.
ComboBoxAction.java


package com.mkyong.common.action;

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

import com.opensymphony.xwork2.ActionSupport;

public class ComboBoxAction extends ActionSupport{

	private List<String> fruits;

	private String yourFruits;
	private String yourMonth;
	
	public String getYourMonth() {
		return yourMonth;
	}

	public void setYourMonth(String yourMonth) {
		this.yourMonth = yourMonth;
	}

	public List<String> getFruits() {
		return fruits;
	}

	public void setFruits(List<String> fruits) {
		this.fruits = fruits;
	}

	public String getYourFruits() {
		return yourFruits;
	}

	public void setYourFruits(String yourFruits) {
		this.yourFruits = yourFruits;
	}

	public ComboBoxAction(){
		
		fruits = new ArrayList<String>();
		fruits.add("Apple");
		fruits.add("Banana");
		fruits.add("Orange");
		fruits.add("Watermelon");
	}

	public String execute() {
		return SUCCESS;
	}
	
	public String display() {
		return NONE;
	}
	
}

2. Result page

Render the combo box via “<s:combobox>” tag, and populate the select options via Java list and OGNL list

combobox.jsp


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

<body>
<h1>Struts 2 <s:combobox> example</h1>

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

<h2>
	<s:combobox label="What's your favor fruit" 
		headerKey="-1" headerValue="--- Select ---"
		list="fruits" 
		name="yourFruits" />
</h2>

<h2>
	<s:combobox label="Select a month" 
		headerKey="-1" headerValue="--- Select ---"
		list="#{'1':'Jan', '2':'Feb', '3':'Mar', '4':'Apr'}" 
		name="yourMonth" />
</h2> 

<s:submit value="submit" name="submit" />
	
</s:form>

</body>
</html>

result.jsp


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

<body>
<h1>Struts 2 <s:combobox> example</h1>

<h2>
  Favor fruit : <s:property value="yourFruits"/>
</h2> 

<h2>
  Selected month : <s:property value="yourMonth"/>
</h2> 

</body>
</html>

3. struts.xml

Link all together ~


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

 <constant name="struts.devMode" value="true" />
 	
<package name="default" namespace="/" extends="struts-default">
		
   <action name="comboBoxAction" 
         class="com.mkyong.common.action.ComboBoxAction" method="display">
	<result name="none">pages/combobox.jsp</result>
   </action>
		
   <action name="resultAction" class="com.mkyong.common.action.ComboBoxAction">
	<result name="success">pages/result.jsp</result>
   </action>

</package>
	
</struts>

5. Demo

http://localhost:8080/Struts2Example/comboBoxAction.action

Struts 2 combo box example

http://localhost:8080/Struts2Example/resultAction.action

Struts 2 combo box example

Reference

  1. Struts 2 combo box documentation
  2. Wiki combo box definition

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
10 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
H KHarikrishna GorrepatiBobVRJon Recent comment authors
newest oldest most voted
Mark Haggerty
Guest
Mark Haggerty

Hi mkyong,
Thank you so much for all of your great examples and help! I am trying to do a combobox or drop list that is pulled from a database. I also need it to display one thing and save another much like your Month example above but the list would come from a query. My list would come from a query similar to this “Select ID, Code From ValidValueTable Where ValidValueListName = ‘Months'”

Code would have “Feb”, “Mar”, “Apr”… and ID is just an auto increamenting numberic Key.

Any help would be greatly appreciated.
Thanks!

H K
Guest
H K

Not able to use tag sql:query with struts2 in JSP. Any recommendation ?

VR
Guest
VR

Hi mkyong,

Thank you for all your work!
I do have a question how can I passe the value of my combobox to the action, when I pick one value and passing it inside a click?
Thank you

nizar
Guest
nizar

hello
there’s 1 error in ComboBoxAction.java
don’t make
list=”fruits”
but

list=”#{‘1′:’banana’, ‘2’:’apple’}”

Bob
Guest
Bob

The point of “fruits” was to pull from a list (i.e. configurable items), not hardcoded items.

HEMENDRA
Guest
HEMENDRA

here the case you have discussed you have used script tags
what i am looking for is without the use of script tags apply this

joy
Guest
joy

Hi, mkyong

I test the combobox , when I choose a option, the textbox will not show the key of the option. why ?

Shruti
Guest
Shruti

Hi, we are developing a project in struts2. We are unable to do event handling in struts. We are pasting our code below please help us to resolve..thats really important at this point.. We want on selection of oemnm(item in combobox) other combobox should be populated with corresponding values in database. We are getting the combobox but no values in it. And if second combobox is selected corresponding values from database should get displayed in textboxes. Please help.. JAVA Codes here… JAVA- Editrsm.java package vaannila; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import com.opensymphony.xwork2.ActionSupport; public… Read more »

shaswat
Guest
shaswat

nice tutorials .Thanx a lot….

dida
Guest
dida

download link is wrong

trackback
Struts 2 autocompleter example

[…] Struts 2 combo box example […]