sareeshmnair
sareeshmnair

Reputation: 431

How to fill the values of <s:select> tag with ArrayList values from the action in Struts 2?

I am developing a simple Struts application. In my JSP I have a dropdown list box (using <s:select> tag). I need to fill the values with ArrayList values in the action class. How can I do that? What changes are needed in the struts.xml file for complete this?

JSP:

<s:select name="department" label="" list="departmentlist"  headerKey="-1" headerValue="Select Department">

Action class:

private List<String> departmentlist = new ArrayList<String>();

public String xyz()
{
    departmentlist.add("aaa");
    departmentlist.add("bbb");
    departmentlist.add("ccc");
    departmentlist.add("ddd");
    return "success";
}

Update:

I got the following error message:

"The requested list key 'departmentlist' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

Upvotes: 1

Views: 4171

Answers (2)

Roman C
Roman C

Reputation: 1

The error

"The requested list key 'departmentlist' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location] "

means that the <s:select> tag is not able to resolve departmentlist as a collection. It is an OGNL expression which is trying to find the departmentlist in the value stack and if it not found or contains a null reference the <s:select> tag will complain. When you render the <s:select> tag make sure the list is in the value stack and is initialized

Upvotes: 2

Andre Kouame
Andre Kouame

Reputation: 139

Try to add get and set method for our field departmentlist,in your class Exple : in your class controller put this method for your field departmentlist :

public  List<String> getDepartmentlist(){
  return this.departmentlist();
}

public void setDepartmentlist(List<String> departmentlist){
  return this.departmentlist = departmentlist;
}

Upvotes: 0

Related Questions