This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSF and the fly likes selectOneMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "selectOneMenu " Watch "selectOneMenu " New topic
Author

selectOneMenu

Frederic Dehedin
Greenhorn

Joined: Feb 27, 2008
Posts: 2
Hello

I'm working on an application (Address Database) using JSF (Base JSF libs with IBM Faces). Apache Tomcat Server 5.5.

Since days i'm trying to solve my problem with h:selectOneMenu and converter. I can save values correctly, but i cannot display the saved value again as a selected item.

so here is my jsf code section:
<h:selectOneMenu value="#{personBean.person.representative}"
styleClass="fldDefault fldcombo">
<f:selectItems value="#{partnerBean.representatives}" />
<f:converter converterId="keywordConvertor" />
<f:attribute name="SelectItemArrayList"
value="#{partnerBean.representatives}" />
</h:selectOneMenu>

representatives are keywords and in the selectOneMenu, a representative keyword object is stored.

here is my partnerBean.jsp Code:

private List<SelectItem> representatives = null;

/**
* populate keywords of type REPRESENTATIVE
*/
public void populateRepresentatives() {
representatives = new ArrayList<SelectItem>();
ArrayList<Keyword> representativeKeywords = kwHandler
.getKeywordsByType(KW_REPRESENTATIVE_TYPE);
for (Keyword keyword : representativeKeywords) {
representatives.add(new SelectItem(keyword, keyword
.getKwDescription()));
}
}

so the function gets the representative keyword objects, loop through them and add a newly created SelectItem Object to the representatives list.

There is a converter in order to save and display (should!!) the values:

public class KeywordConvertor implements Converter {

public Object getAsObject(FacesContext context, UIComponent component,
String value) {
....code for saving...
}

public String getAsString(FacesContext context, UIComponent component,
Object obj) {
`System.out.println(((Keyword) obj).getId()+"");
return ((Keyword) obj).getId()+"";
}
}

i did not provide the code for getAsObject, since the saving of the combo box works fine. If it has to do something with my problem, please ask me for this code..

The System.out.prinln prints all ids correctly, but the generated HTML Code does not select the given value in the combobox.

So whats wrong with the getAsString Method? or is the function completely somewhere else?
I have to say that the function ((Keyword) obj).getId() returns a long !! is that the problem?

Any help is very appreciated
Frederic Dehedin
Greenhorn

Joined: Feb 27, 2008
Posts: 2
I think i know now where the problem is:

the value of person.getRepresentative function returns a keyword, when i printout the hashCode of the keyword object, i get e.g. 55336644

if i check the objects for the hashCode loaded in the SelectOne Combobox (printout in the KeywordConvertor.getAsString() method), i get a different hashCode for this selection!!! So there are two different objects and JSF doesn't recognise the selection of the different objects, although they have the same values.. is this correct?

By the way, we work with hibernate..

i tried also a function in the Keyword Class hashCode, which returns the id of the Database, in order to have the same ids in the objects, but no success

Anybody a solution?

If i don't find any, i forget working with objects in the selectOne Combobox but working with strings only, in the converter i get the keyword object again with the provided id. But if anyody has another solution, it would be very appreciated..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: selectOneMenu