Whenever you start working in struts2 tag select box so might be get the
following error but you might be helpless to solve following error.
Today i will give you the detail description about the select box.
There may be the following criteria to display record in select box in real application world.
1. Show ArrayList data in select box
2. Show Map data in select box
3. Show Java Beans in select box
I will go step by step so you can get better idea to use it.
1. Show ArrayList Data in Select Box :
a). You have to create an ArrayList Object in your STRUTS Action java class and create its getter/setter method.
b). Now you have to fill the arrayList Object in your action calling method.
c). Now use the select box tags in your jsp page.
2. Show Map data in select box
a). You have to create an Map Object in your STRUTS Action java class and create its getter/setter method.
b). Now you have to fill the Map Object in your action calling method.
c). Now use the select box tags in your jsp page.
3. Show Java Beans in select box
a) First you have to create a java bean class .
p
b). You have to create an JavaBean Object in your STRUTS Action java class and create its getter/setter method.
c). Now you have to fill the JavaBean Object in your action calling method.
d). Now use the select box tags in your jsp page.
Now we will add the submit button that will show you the selected items from the select box.
a) Added Save method in struts Action.
b) added the following below code at your jsp page.
Please Note : select box name and define property name is action class should be same.
Now we are just mixing all code
Struts2.xml file :
Java Action Class :
Java Bean Class:
Your JSP Page
Thank you so much to reading this article. If you have any query so feel free to ask.
ERROR [[jsp]] Servlet.service() for servlet jsp
threw exception tag 'select', field 'list', name 'listShowName': The
requested list key 'listForShow' could not be resolved as a
collection/array/map/enumeration/iterator type. Example: people or
people.{name} - [unknown location]
Today i will give you the detail description about the select box.
There may be the following criteria to display record in select box in real application world.
1. Show ArrayList data in select box
2. Show Map data in select box
3. Show Java Beans in select box
I will go step by step so you can get better idea to use it.
1. Show ArrayList Data in Select Box :
a). You have to create an ArrayList Object in your STRUTS Action java class and create its getter/setter method.
public
class
MySelectBox
extends
ActionSupport {
private
List<String> valueForSelect;
public
List<String> getValueForSelect() {
return
valueForSelect;
}
public
void
setValueForSelect(List<String> valueForSelect) {
this
.valueForSelect = valueForSelect;
}
}
public
String selectMethod() {
valueForSelect =
new
ArrayList<String>();
valueForSelect.add(
"Value 1"
);
valueForSelect.add(
"Value 2"
);
valueForSelect.add(
"Value 3"
);
valueForSelect.add(
"Value 4"
);
valueForSelect.add(
"Value 5"
);
valueForSelect.add(
"Value 6"
);
valueForSelect.add(
"Value 7"
);
return
SUCCESS;
}
c). Now use the select box tags in your jsp page.
<s:select label=
"Select Day"
name=
"usingArrayList"
headerKey=
"1"
headerValue=
"-- Please Select --"
list=
"valueForSelect"
theme=
"simple"
/>
2. Show Map data in select box
a). You have to create an Map Object in your STRUTS Action java class and create its getter/setter method.
public
class
MySelectBox
extends
ActionSupport {
private
Map<Integer, String> mapForSelect;
public
Map<Integer, String> getMapForSelect() {
return
mapForSelect;
}
public
void
setMapForSelect(Map<Integer, String> mapForSelect) {
this
.mapForSelect = mapForSelect;
}
}
public
String selectMethod() {
mapForSelect =
new
HashMap<Integer, String>();
mapForSelect.put(
1
,
"map 1"
);
mapForSelect.put(
2
,
"map 2"
);
mapForSelect.put(
3
,
"map 3"
);
mapForSelect.put(
4
,
"map 4"
);
mapForSelect.put(
5
,
"map 5"
);
mapForSelect.put(
6
,
"map 6"
);
return
SUCCESS;
}
<s:select label=
"Select Day"
name=
"usingMap"
list=
"mapForSelect"
theme=
"simple"
headerKey=
"1"
headerValue=
"-- Please Select --"
/>
3. Show Java Beans in select box
a) First you have to create a java bean class .
p
ackage
com.example.actions;
public
class
SelectBean {
private
Integer id;
private
String username;
private
String password;
public
Integer getId() {
return
id;
}
public
void
setId(Integer id) {
this
.id = id;
}
public
String getUsername() {
return
username;
}
public
void
setUsername(String username) {
this
.username = username;
}
public
String getPassword() {
return
password;
}
public
void
setPassword(String password) {
this
.password = password;
}
}
public
class
MySelectBox
extends
ActionSupport {
private
List<SelectBean> listForShow =
new
ArrayList<SelectBean>();
public
List<SelectBean> getListForShow() {
return
listForShow;
}
public
void
setListForShow(List<SelectBean> listForShow) {
this
.listForShow = listForShow;
}
}
public
String selectMethod() {
SelectBean selectBean1 =
new
SelectBean();
selectBean1.setId(
2
);
selectBean1.setUsername(
"aoi blog2"
);
selectBean1.setPassword(
"jigar nagar"
);
SelectBean selectBean2 =
new
SelectBean();
selectBean2.setId(
3
);
selectBean2.setUsername(
"aoi blog3"
);
selectBean2.setPassword(
"jigar nagar"
);
listForShow.add(selectBean);
listForShow.add(selectBean1);
listForShow.add(selectBean2);
return
SUCCESS;
}
d). Now use the select box tags in your jsp page.
<s:select list=
"listForShow"
listKey=
"id"
listValue=
"username"
name=
"usingJavaBean"
theme=
"simple"
headerKey=
"1"
headerValue=
"-- Please Select --"
/>
Now we will add the submit button that will show you the selected items from the select box.
a) Added Save method in struts Action.
public
class
MySelectBox
extends
ActionSupport {
private
String usingArrayList;
private
String usingMap;
private
String usingJavaBean;
public
String getUsingArrayList() {
return
usingArrayList;
}
public
void
setUsingArrayList(String usingArrayList) {
this
.usingArrayList = usingArrayList;
}
public
String getUsingMap() {
return
usingMap;
}
public
void
setUsingMap(String usingMap) {
this
.usingMap = usingMap;
}
public
String getUsingJavaBean() {
return
usingJavaBean;
}
public
void
setUsingJavaBean(String usingJavaBean) {
this
.usingJavaBean = usingJavaBean;
}
public
String saveMethod() {
System.out.println(
"usingArrayList "
+usingArrayList);
System.out.println(
"usingJavaBean "
+usingJavaBean);
System.out.println(
"usingMap "
+usingMap);
selectMethod();
return
SUCCESS;
}
b) added the following below code at your jsp page.
<s:property value=
"usingArrayList"
/><br/>
<s:property value=
"usingMap"
/><br/>
<s:property value=
"usingJavaBean"
/>
Now we are just mixing all code

Struts2.xml file :
<!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
=
"struts2"
extends
=
"struts-default"
namespace
=
"/"
>
<
action
name
=
"listForSelect"
class
=
"com.example.actions.MySelectBox"
method
=
"selectMethod"
>
<
result
name
=
"success"
>/select.jsp</
result
>
</
action
>
<
action
name
=
"saveSelect"
class
=
"com.example.actions.MySelectBox"
method
=
"saveMethod"
>
<
result
name
=
"success"
>/select.jsp</
result
>
</
action
>
</
package
>
</
struts
>
Java Action Class :
package
com.example.actions;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
com.opensymphony.xwork2.ActionSupport;
public
class
MySelectBox
extends
ActionSupport {
private
List<String> valueForSelect;
private
Map<Integer, String> mapForSelect;
private
List<SelectBean> listForShow =
new
ArrayList<SelectBean>();
private
String usingArrayList;
private
String usingMap;
private
String usingJavaBean;
public
String selectMethod() {
System.out.println(
"Hello I m calling"
);
valueForSelect =
new
ArrayList<String>();
valueForSelect.add(
"Value 1"
);
valueForSelect.add(
"Value 2"
);
valueForSelect.add(
"Value 3"
);
valueForSelect.add(
"Value 4"
);
valueForSelect.add(
"Value 5"
);
valueForSelect.add(
"Value 6"
);
valueForSelect.add(
"Value 7"
);
mapForSelect =
new
HashMap<Integer, String>();
mapForSelect.put(
1
,
"map 1"
);
mapForSelect.put(
2
,
"map 2"
);
mapForSelect.put(
3
,
"map 3"
);
mapForSelect.put(
4
,
"map 4"
);
mapForSelect.put(
5
,
"map 5"
);
mapForSelect.put(
6
,
"map 6"
);
SelectBean selectBean =
new
SelectBean();
selectBean.setId(
1
);
selectBean.setUsername(
"aoi blog1"
);
selectBean.setPassword(
"jigar nagar"
);
SelectBean selectBean1 =
new
SelectBean();
selectBean1.setId(
2
);
selectBean1.setUsername(
"aoi blog2"
);
selectBean1.setPassword(
"jigar nagar"
);
SelectBean selectBean2 =
new
SelectBean();
selectBean2.setId(
3
);
selectBean2.setUsername(
"aoi blog3"
);
selectBean2.setPassword(
"jigar nagar"
);
listForShow.add(selectBean);
listForShow.add(selectBean1);
listForShow.add(selectBean2);
return
SUCCESS;
}
public
String getUsingArrayList() {
return
usingArrayList;
}
public
void
setUsingArrayList(String usingArrayList) {
this
.usingArrayList = usingArrayList;
}
public
String getUsingMap() {
return
usingMap;
}
public
void
setUsingMap(String usingMap) {
this
.usingMap = usingMap;
}
public
String getUsingJavaBean() {
return
usingJavaBean;
}
public
void
setUsingJavaBean(String usingJavaBean) {
this
.usingJavaBean = usingJavaBean;
}
public
String saveMethod() {
System.out.println(
"usingArrayList "
+usingArrayList);
System.out.println(
"usingJavaBean "
+usingJavaBean);
System.out.println(
"usingMap "
+usingMap);
selectMethod();
return
SUCCESS;
}
public
List<SelectBean> getListForShow() {
return
listForShow;
}
public
void
setListForShow(List<SelectBean> listForShow) {
this
.listForShow = listForShow;
}
public
List<String> getValueForSelect() {
return
valueForSelect;
}
public
void
setValueForSelect(List<String> valueForSelect) {
this
.valueForSelect = valueForSelect;
}
public
Map<Integer, String> getMapForSelect() {
return
mapForSelect;
}
public
void
setMapForSelect(Map<Integer, String> mapForSelect) {
this
.mapForSelect = mapForSelect;
}
}
Java Bean Class:
package
com.example.actions;
public
class
SelectBean {
private
Integer id;
private
String username;
private
String password;
public
Integer getId() {
return
id;
}
public
void
setId(Integer id) {
this
.id = id;
}
public
String getUsername() {
return
username;
}
public
void
setUsername(String username) {
this
.username = username;
}
public
String getPassword() {
return
password;
}
public
void
setPassword(String password) {
this
.password = password;
}
}
Your JSP Page
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
><
title
>Select Box Example - AOI Blogs</
title
></
head
>
<
body
>
<
center
>
<
h1
>Select Box Example</
h1
>
</
center
>
<
s:form
action
=
"saveSelect"
method
=
"post"
namespace
=
"/"
>
<
table
cellpadding
=
"2"
cellspacing
=
"2"
border
=
"1"
align
=
"center"
>
<
tr
>
<
td
>Using Array List</
td
>
<
td
><
s:select
label
=
"Select Day"
name
=
"usingArrayList"
headerKey
=
"1"
headerValue
=
"-- Please Select --"
list
=
"valueForSelect"
theme
=
"simple"
/></
td
>
</
tr
>
<
tr
>
<
td
>Using Map</
td
>
<
td
><
s:select
label
=
"Select Day"
name
=
"usingMap"
list
=
"mapForSelect"
theme
=
"simple"
headerKey
=
"1"
headerValue
=
"-- Please Select --"
/></
td
>
</
tr
>
<
tr
>
<
td
>Using Java Beans</
td
>
<
td
><
s:select
list
=
"listForShow"
listKey
=
"id"
listValue
=
"username"
name
=
"usingJavaBean"
theme
=
"simple"
headerKey
=
"1"
headerValue
=
"-- Please Select --"
/> </
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
align
=
"center"
>
<
s:submit
value
=
"SUBMIT"
theme
=
"simple"
></
s:submit
>
</
td
>
</
tr
>
</
table
>
</
s:form
>
<
s:property
value
=
"usingArrayList"
/><
br
/>
<
s:property
value
=
"usingMap"
/><
br
/>
<
s:property
value
=
"usingJavaBean"
/>
</
body
>
</
html
>
Thank you so much to reading this article. If you have any query so feel free to ask.