Select Many Problem : JSF

After few days, got some time to write a post… well as i promised in my earlier post.. I thought of writing about the Annoying problem anybody will face while using selectMany component in JSF.

At 1st with out any experience what any one would do is writing both assessor and mutator methods to return and set A LIST of selected objects. some thing like

private List<myClass> selectList;

public List<myClass> getSelectList() {
return selectList;
}

public void setSelectList(List<myClass> selectList) {
this.selectList = selectList;
}

Even though this is the straight forward way, For some reason JSF implementation does not support it. In many places over the NET and in JSF forums, people have advised to use String Arrays, Saying you cannot use Java Collections in this scenario.

But Use of String Arrays are very much annoying and makes your work very messy. After some testing and trying I found this work around to take the selected objects as it is, not just the label string, Its pretty simple. Instead of using String arrays, Just use an array of your own class. Something like this

private myClass[] selectList;

public myClass[] getSelectList() {
return selectList;
}

public void setSelectList(myClass[] selectList) {
this.selectList = selectList;
}

Of-cause you have to use a converter in this case but not difference out there its just a normal converter for your class. So hope this tip will help

cheers !!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s