Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSF and the fly likes <h:selectOneMenu issue defaulting to specific selectItem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "<h:selectOneMenu issue defaulting to specific selectItem" Watch "<h:selectOneMenu issue defaulting to specific selectItem" New topic
Author

<h:selectOneMenu issue defaulting to specific selectItem

Mary Joe
Greenhorn

Joined: Jun 17, 2010
Posts: 20
Hi,

I have got a selectOnemenu with selectItems populated from an Enum - CodeTyeEnum.
If a code is already saved then the dropdown should default to that code, if no code is saved for that Period a 'Please Select' will be the default value.

Currently what happens is the first time I enter the screen for a Period if the saved code is 'N' then the default value selected and highlighted is 'N'.
If I go back and re enter the screen for a Period with saved code X it is still highlighting 'N' instead of 'X', can anyone see what the issue is?

Following is the xhtml snippet

<h:selectOneMenu id="confirmCode"
binding="#{detail.myCode}"
converter="myCodeConverter"
disabled="#{detail.readOnly}">

<f:selectItems id="myCodeOptions"
value="#{detail.myCodeOptions}" />
</h:selectOneMenu>


The backing bean method is as is as follows

public List getMyCodeOptions() {

Long selectedId = null;
if (getPeriods().size() > 1) {

if (getSelectedPeriodId() == null && "".equals(getSelectedPeriodId())) {

return EnumerationSelectItems.getSelectItems(CodeTypeEnum.class, true, true);
} else {
if (getSelectedPeriodId() != null) {

selectedId = new Long(getSelectedPeriodId());
}
}
} else {

selectedId = getPeriod().getId();

}
if (selectedId == null) {

return EnumerationSelectItems.getSelectItems(CodeTypeEnum.class, true, true);
}

PeriodService service = (PeriodService ) getSpringBean(DATA_SERVICE);
Period period = service.getPeriod(selectedId);
CodeTypeEnum code = period.getCode();
if (code != null) {
boolean isSuspended = ((CodeTypeEnum.SUSPENDED.equals(code))
&& (period.getState() == StateEnum.CONFIRMED));

if ((period.isOnWorklist() && (period.getState() == StateEnum.CONFIRMATION_PENDED))
|| (period.getState() == StateEnum.AWAITING_REASSESSMENT)
|| isSuspended) {
return convertEnumsToSelectItems(CodeTypeEnum.class, code);
}
}

return EnumerationSelectItems.getSelectItems(CodeTypeEnum.class, true, true);
}

/**
*
* @param enumClass
* @param attendanceCode
* @return List of selectItems
*/
private static List convertEnumsToSelectItems(Class enumClass, Enum code) {
List enumList = EnumUtils.getEnumList(CodeTypeEnum.class);
List selectItems = new ArrayList();
String defaultValue = ((StringValuedEnum) code).getValue();

SelectItem selectItem = new SelectItem(code,
defaultValue,
defaultValue);
selectItems.add(selectItem);

Iterator it = enumList.iterator();
while (it.hasNext()) {
Enum enumeration = (Enum) it.next();
String value = null;

StringValuedEnum stringValuedEnum = (StringValuedEnum) enumeration;
value = stringValuedEnum.getValue();
if (!value.equals(defaultValue)) {
SelectItem item = new SelectItem(enumeration,
value,
value);
selectItems.add(item);
}
}

return selectItems;
}
suresh dasari
Ranch Hand

Joined: Oct 05, 2009
Posts: 121

I am not seeing any value attribute for <h:selectOneMenu /> tag, without the value attribute how come the controller will hold the previously selected values which is already saved.
try to use value attribute and map it to the backing bean which will retrieve the value from the backend that is already saved, if nothing is there empty will be return.


Sun Certified Java Programmer with 93 percent
Mary Joe
Greenhorn

Joined: Jun 17, 2010
Posts: 20
Thanks that was the issue
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <h:selectOneMenu issue defaulting to specific selectItem