Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

<h:selectOneMenu issue defaulting to specific selectItem

 
Mary Joe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mary Joe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that was the issue
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic