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