Thursday, 15 April 2010

JSF 2 enum property lost after bean validation -



JSF 2 enum property lost after bean validation -

using jsf2 (myfaces 2.1) , cdi (weld) utilize selectonemenu filled enum conditionally render sec selectonemenu (doublemenu in code). there several other fields in form validated using bean validation.

the problem:

if there validation errors after clicking on commandbutton , error messages shown, doublemenu not reappear. appears bean.type =='double' not true. why? selectonemenu shows alternative double selected item.

@javax.inject.named @javax.enterprise.context.requestscoped public class bean { private enum myenum { single, double } private myenum type; public myenum gettype() { homecoming type; } public void settype(myenum type) { this.type = type; } public myenum [] gettypes() { homecoming myenum.values(); } <h:form> <h:selectonemenu value="#{bean.type}"> <f:selectitems value="#{bean.types}" /> <f:ajax render="doublemenu" /> </h:selectonemenu> <h:panelgroup id="doublemenu"> <h:panelgroup rendered="#{bean.type == 'double'}"> <h:selectonemenu ...> </h:selectonemenu> </h:panelgroup> </h:panelgroup> <h:inputtext id="validated"/> <h:commandbuttonaction="#{bean.save}" </h:form>

btw: bean.save not executed

it's because bean request scoped. request scoped beans garbaged end of every request , newly created on begin of every request. applies individual ajax requests on same view.

the rendered attribute evaluated during collecting submitted values (the apply request values phase). however, @ point submitted values collected, #{bean.type} not been set yet (it has still set based on collected submitted value during update model values phase). bean request scoped, homecoming default value, not submitted value previous request.

there 2 ways prepare this.

put bean in bit broader scope. jsf offers @viewscoped purpose. in current jsf 2.1 version not compatible cdi. if switching jsf @managedbean not option, you'd need myfaces codi bridge jsf @viewscoped transparently cdi, or wait jsf 2.2 cdi compatible @viewscoped out box.

check request parameter instead of bean property.

<h:selectonemenu binding="#{type}" ...> ... </h:selectonemenu> <h:panelgroup rendered="#{param[type.clientid] == 'double'}">

please note unrelated enums. you'd have had same problem when using e.g. string. please note fixed typo in code example, enum value of double not same double.

see also: how take right bean scope? h:commandlink / h:commandbutton not beingness invoked

jsf enums bean-validation

No comments:

Post a Comment