File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts 2 checkboxlist error - list key could not be resolved Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 checkboxlist error - list key could not be resolved" Watch "Struts 2 checkboxlist error - list key could not be resolved" New topic
Author

Struts 2 checkboxlist error - list key could not be resolved

James Hollier
Greenhorn

Joined: Jul 27, 2009
Posts: 19
I have a simple Form working in Struts2. But when I add the following
to the form, it fails:

<s:checkboxlist list="communityList" name="community"
label="Community" />


The error I get is this:


tag 'checkboxlist', field 'list', name 'community': The requested list
key 'communityList' could not be resolved as a collection/array/map/
enumeration/iterator type. Example: people or people.{name}


Apparently, my action class is NOT providing the List data requested
by the checkboxlist. Like I said, without the List type tag then
everything works fine! MUST be something simple I'm overlooking, but
this code came straight out of an example from vaanilla.com. Here are
the relavent components:


struts.xml snippet:


<action name="*Register" method="{1}"
class="struts2.example3.RegisterAction">
<result name="populate">/example3Register.jsp</result>
<result name="input">/example3Register.jsp</result>
<result name="success">/example3Success.jsp</result>
</action>


example3Register.jsp snippet:


<s:form action="Register">
<s:textfield name="userName" label="User Name" />
<s:password name="password" label="Password" />
<s:radio name="gender" label="Gender" list="{'Male','Female'}" />
<s:textarea name="about" label="About You" />
<s:checkboxlist list="communityList" name="community"
label="Community" />
<s:checkbox name="mailingList" label="Would you like to join our
mailing list?" />
<s:submit />
</s:form>


Action class:


package struts2.example3;


/**
* http://www.vaannila.com/struts-2/struts-2-example/struts-2-ui-tags-ex...
*/
public class RegisterAction extends ActionSupport {
private String userName;
private String password;
private String gender;
private String about;
private String country;
private ArrayList<Country> countryList;
private String[] community;
private ArrayList<String> communityList;
private Boolean mailingList;


public static class Country {
private int countryId;
private String countryName;


public Country(int countryId, String countryName) { this.countryId =
countryId; this.countryName = countryName; }


public int getCountryId() { return countryId; }
public String getCountryName() { return countryName; }
}


public String populate() {
countryList = new ArrayList<Country>();
countryList.add(new Country(1, "India"));
countryList.add(new Country(2, "USA"));
countryList.add(new Country(3, "France"));
communityList = new ArrayList<String>();
communityList.add("Java");
communityList.add(".Net");
communityList.add("SOA");
community = new String[]{"Java",".Net"};
mailingList = true;
return "populate";
}


public void prepare() throws Exception {
countryList = new ArrayList<Country>();
communityList = new ArrayList<String>();
}


public String execute() { return SUCCESS; }


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 String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }


public String getAbout() { return about; }
public void setAbout(String about) { this.about = about; }


public String getCountry() { return country; }
public void setCountry(String country) { this.country = country; }


public ArrayList<Country> getCountryList() { return countryList; }
public void setCountryList(ArrayList<Country> countryList)
{ this.countryList = countryList; }


public String[] getCommunity() { return community; }
public void setCommunity(String[] community) { this.community =
community; }


public ArrayList<String> getCommunityList() { return
communityList; }
public void setCommunityList(ArrayList<String> communityList)
{ this.communityList = communityList; }


public Boolean getMailingList() { return mailingList; }
public void setMailingList(Boolean mailingList) { this.mailingList =
mailingList; }



}


Thanks, J
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags; that's illegible.
James Hollier
Greenhorn

Joined: Jul 27, 2009
Posts: 19
Re-Post with Code tags...

I have a simple Form working in Struts2. But when I add the following
to the form, it fails:



The error I get is this:

tag 'checkboxlist', field 'list', name 'community': The requested list
key 'communityList' could not be resolved as a collection/array/map/
enumeration/iterator type. Example: people or people.{name}

Apparently, my action class is NOT providing the List data requested
by the checkboxlist. Like I said, without the List type tag then
everything works fine! MUST be something simple I'm overlooking, but
this code came straight out of an example from vaanilla.com. Here are
the relavent components:

struts.xml snippet:



example3Register.jsp snippet:



Action class:


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What URL are you accessing the form with?
James Hollier
Greenhorn

Joined: Jul 27, 2009
Posts: 19
I am invoking the above supplied page as the url - /example3Register.jsp. I expect this page to render the form, using the class to provide defaults. This works fine except for this "list" field!

Thanks, J
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you're not executing the action I'm not sure why you'd expect there to be a list prepared for the JSP--that happens in the action.

As configured you'd need to use populateRegister.action as the URL.
James Hollier
Greenhorn

Joined: Jul 27, 2009
Posts: 19
Thanks, you've shown me greenhorn the light! J
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No prob; I've been planning on revamping some error messages to make them more useful but haven't gotten around to it yet. Maybe this will spur me on ;)
Marc Heruela
Ranch Hand

Joined: Jul 23, 2009
Posts: 38
David Newton wrote:No prob; I've been planning on revamping some error messages to make them more useful but haven't gotten around to it yet. Maybe this will spur me on ;)


Hello.

Sorry but I'm afraid I didnt follow the discussion. I have a similar issue. Here is my scenario:

Page A: views all records in the form of table. User can chose edit the records listed by ticking a radio button and clicking submit. On click of submit, User is directed to Page B.
Page B: displays a form of the selected record. He can edit and submit his changes. On click of submit, it must pass some custom validations and then if successful, goes back to page A.

My problem is, this scenario is working well before i added the select tag on Page B. When I submit a valid form of Page B, the transaction is completed without error, but when the submitted form does not pass my custom validation, supposedly it should stay on Page B and display the error there. Instead it displays this error:



Here's my struts.xml:



from above, page A is listAllEmployees.jsp. on submit, it retrieves the selected record's details thus performing action retrieveProfile if no error, it displays page B,
which is editEmployee.jsp. When user is done editing, if success,it goes back to page A, if fail it must stay on page B.

Here are my jsp files:

-listAllEmployees.jsp


-editEmployee.jsp


My action classes:
-UserAction..java




thanks:
-marckun


-marckun
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's too much code to wade through.

Do you create a list of roles for every pge that needs one?
Marc Heruela
Ranch Hand

Joined: Jul 23, 2009
Posts: 38
David Newton wrote:That's too much code to wade through.

Do you create a list of roles for every pge that needs one?


ooppss sorry.. i agree that is too much code.. hmm

but any way. i changed my entire action classes, i fused them since

the action classes above is accessing the USER table. i fused all transaction to USER table to one action class.

is it a good practice in struts2 (eg: 1 entity = 1 action class)?

im sorry im new to this framework and such am not so familiar with how to design my action classes effciently. :D

anyways, to answer your query above, nope i dont need the list of roles in every page.

i only need it in editEmployee.jsp page (the user form).

iv sorta solve this problem by implementing Preparable in my action class and in my prepare method i

retrieved all Roles in the database:

eg:



but im not so comfortable with this approach, since based on how i understand it,

every invokation of this UserAction class will have to invoke prepare method first

and in turn must query to the database, which is not necessary unless the page i am rendering is the editEmployee.jsp page.

here's part of my struts.xml



here's my jsp:



i am accessing the form above by clicking a button (either add or edit). if edit button is clicked, the action performed is userForm (please see struts.xml),

meanwhile if add is clicked the action performed is addNew (please see struts.xml).

here's part of my UserAction.java:



again, if i remove select tag in my form, everything works fine.

hope you can shed a light .. thanks

-marckun
Raja mca
Greenhorn

Joined: Feb 29, 2012
Posts: 2
I got an error when i had using checkboxlist tag of struts2

Servlet.service() for servlet jsp threw exception
tag 'checkboxlist', field 'list', name 'yourColor': The requested list key 'colors' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

please resolve the problem

Here my code for welcome.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="/struts-tags" prefix="s"%>
<%@taglib uri="/struts-dojo-tags" prefix="sx" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="register">

<s:textfield name="username" key="label.username" />
<s:password name="password" key="label.password" />
<s:radio name="gender" key="label.gender" list="{'Male','Female'}" />


<s:textarea name="about" key="label.tarea" />

<h4>
<s:checkboxlist label="What's your favor color" list="colors"
name="yourColor" value="defaultColor" />
</h4>
<s:checkbox name="mailingList" key="label.cbox" value="true"/>

<s:submit key="label.register" />
</s:form>
</body>
</html>

RegisterAction.java
=============

package com.aquila;

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

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {
private String username;
private String password;
private String gender;
private String about;
private String country;
private List<String> colors;

public List<String> getColors() {
return colors;
}

public void setColors(List<String> colors) {
this.colors = colors;
}

public RegisterAction(){
colors = new ArrayList<String>();
colors.add("red");
colors.add("yellow");
colors.add("blue");
colors.add("green");
}

public String[] getDefaultColor(){
return new String [] {"red", "green"};
}

public String getYourColor() {
return yourColor;
}

public void setYourColor(String yourColor) {
this.yourColor = yourColor;
}

private String yourColor;


private String[] community;

private Boolean mailingList;

public String getValues()throws Exception {


System.out.println("hai......");
community = new String[] { "JAVA", ".NET" };
mailingList = true;

return "populate";
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("hello*******************************************");
return SUCCESS;
}

public String display() {
return NONE;
}

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 String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getAbout() {
return about;
}

public void setAbout(String about) {
this.about = about;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String[] getCommunity() {
return community;
}

public void setCommunity(String[] community) {
this.community = community;
}

public Boolean getMailingList() {
return mailingList;
}

public void setMailingList(Boolean mailingList) {
this.mailingList = mailingList;
}
}
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

use this Preparable Interceptor for pre populating data.


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2 checkboxlist error - list key could not be resolved
 
Similar Threads
jsp
Cannot find bean in any scope
My Small Web Project - but has some problems - please help ...
JspException: Failed to obtain specified collection for Struts:optionscollection
How to display a list box with values from an "Array List" in "Struts2"