File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

<h:selectOneMenu issue defaulting to specific selectItem

Mary Joe

Joined: Jun 17, 2010
Posts: 20

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"

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

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,

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

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

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

Joined: Jun 17, 2010
Posts: 20
Thanks that was the issue
I agree. Here's the link:
subject: <h:selectOneMenu issue defaulting to specific selectItem
It's not a secret anymore!