selectItems can't work with converter in h:selectOneMenu, got message "value is not valid".
leon850515 Feb 23, 2009 6:59 PMWhen I use s:selectItems and f:converter in a h:selectOneMenu, it can't work as expected.
My code is as belows:
Profile Edit Backing Bean
@Name("profileController") @Scope(ScopeType.PAGE) @Restrict("#{identity.loggedIn}") public class ProfileController { @In private IProfileService profileService; @In private IWaySOService waySOService; @RequestParameter private Integer profileId; private Profile profile; List<Processor> processorList; @Create public void init() { profile = profileService.getProfile(profileId); processorList = waySOService.getProcessorList(); } public List<Processor> getProcessorList() { return processorList; } public Profile getProfile() { return profile; } }
Profile Edit Page:
<h:form id="profile"> <h:selectOneMenu id="processorSelect" value="#{profileController.profile.processor}"> <s:selectItems value="#{profileController.processorList}" var="processor" label="#{processor.name}" /> <f:converter converterId="processorConverter" /> <a:support event="onchange" reRender="processorName" ajaxSingle="true" /> </h:selectOneMenu> <h:outputText id="processorName" value="#{profileController.profile.processor.name}" /> </h:form>
Processor Converter:
@Name("processorConverter") @BypassInterceptors @Converter public class ProcessorConverter implements javax.faces.convert.Converter { public Object getAsObject(FacesContext arg0, UIComponent arg1, String content) { IWaySOService waySOService = (IWaySOService) Component.getInstance("waySOService"); return waySOService.getProcessor(Integer.parseInt(content)); } public String getAsString(FacesContext arg0, UIComponent arg1, Object content) { Processor processor = (Processor) content; return String.valueOf(processor.getId()); } }
I just need to select a processor for a profile using drop down list.
profile = profileService.getProfile(profileId); //This method will load profile from the database by the given profile Id processorList = waySOService.getProcessorList(); //This method will load all processors from the database.
For example:
I have a profile which has the processor B. And the processor list includs processor A, B, C.
1.When you first enter into the profile edit page, the drop down shows processor A there, it should be processor B. Only I manually change the processor list with processor B in the first place, it will show processor B. So seems it will always show the first element in the processor list, not show the profile's processor.
2.When I select processor A and C, it works as expected. But when I select the processor B, I got message value is not valid
.
By the way, I use seam management persistence context and has overriden the hashCode and equals method.
Does anyone can give some advice on that?