File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes How to add instances of SelectItem as Argument to method    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to add instances of SelectItem as Argument to method    " Watch "How to add instances of SelectItem as Argument to method    " New topic
Author

How to add instances of SelectItem as Argument to method

Bansilal Haudakari
Greenhorn

Joined: Jun 28, 2004
Posts: 15
The following code in Add Role method results in error
public String addRole() {
deviceRoles.addAll(selectedUserRoles);
return null;
}

ERROR:
javax.faces.FacesException: Collection referenced by UISelectItems with binding

'#{test.deviceRoles}' and Component-Path : {Component-Path : [Class:

org.ajax4jsf.framework.ajax.AjaxViewRoot,ViewId: /page.jsp][Class:

javax.faces.component.html.HtmlForm,Id: _id0][Class:

org.ajax4jsf.ajax.html.HtmlAjaxOutputPanel,Id: _id6][Class:

javax.faces.component.html.HtmlPanelGrid,Id: _id13][Class:

javax.faces.component.html.HtmlSelectManyListbox,Id: _id19][Class:

javax.faces.component.UISelectItems,Id: _id20]} <b>does not contain Objects of type

SelectItem
</b>


I fixed the error by hardcoding the values into Add Role method as shown below

public String addRole() {

deviceRoles.add(new SelectItem("10"," Admin"));
deviceRoles.add(new SelectItem("20"," Focal"));
deviceRoles.add(new SelectItem("30","Architect"));

return null;
}

So I guess the call to "selectedUserRoles" in deviceRoles.addAll(selectedUserRoles); should

return instances of SelectItem with Id and Value as arguments.

That means whenever users selects multiple values from user roles h:selectManyList box, the setselectedUserRoles() method should recieve both Id and Value as instance of Select Item.

Hence i changed the declaration to
private List selectedUserRoles = new ArrayList<SelectItem>();

But not sure how to change the defination of setselectedUserRoles() method to recieve instances of SelectItem with Id and Value as Arguments. Here is the defination

public void setSelectedUserRoles(List selectedUserRoles) {
this.selectedUserRoles = selectedUserRoles;
System.out.println("Setter this.selectedUserRoles "+ this.selectedUserRoles);}




Any pointers/suggestions will be highly appreciated in this regard.
Here is the code snippet in backing bean :

--------------------------------
TestBean.java (Backing Bean)
------------------------------------
private List<Role> userRoles;
private List deviceRoles = new ArrayList<SelectItem>();
private List selectedUserRoles = new ArrayList<SelectItem>();
private List selectedDeviceRoles = new ArrayList<SelectItem>();


public List getSelectedUserRoles() {
System.out.println("Getter this.selectedUserRoles"+selectedUserRoles);
return selectedUserRoles;
}



public void setSelectedUserRoles(List selectedUserRoles) {
this.selectedUserRoles = selectedUserRoles;
System.out.println("Setter this.selectedUserRoles "+ this.selectedUserRoles);}


public String addRole() {
deviceRoles.addAll(selectedUserRoles);
return null;
}

Regards
Bansi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add instances of SelectItem as Argument to method