• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Struts 2 checkboxlist error - list key could not be resolved

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags; that's illegible.
 
James Hollier
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What URL are you accessing the form with?
 
James Hollier
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, you've shown me greenhorn the light! J
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ;)
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use this Preparable Interceptor for pre populating data.
 
Police line, do not cross. Well, this tiny ad can go through:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic