jsf - Applying request values to entity bean loaded with id from inputHidden before other fields -
i have facelet template with:
<f:metadata> <o:viewparam name="id" value="#{homebean.id}" /> </f:metadata> <h:form> <h:inputhidden value="#{homebean.id}" /> <h:inputtext value="#{homebean.user.firstname}" /> <h:commandbutton value="submit" action="#{homebean.onsave()}" /> </h:form> and request scoped bean with:
@named @requestscoped public class homebean { private integer id; private user user; public void setid(integer id) { system.out.println("setid called"); user = // code loading user entity bean supplied id } // other accessors id , user } initial page load works well, entity loaded , displayed in form, inputhidden set entity id. problem submit throws:
javax.el.propertynotfoundexception - target unreachable, base expression '. user' resolved null probably because getuser called before setid. how can solve this? have request scoped bean, know can solved @ least viewaccess scoped bean.
edit: noticed exception thrown in process validations phase, thought exception thrown in update model values phase. changed "private user" "private user user = new user()" , it's ok, feels little weird.
regards, pavel
the omnifaces <o:viewparam> sets request parameter in initial request , not in postbacks. intented used @viewscoped beans request parameter isn't unnecessarily been validated, converted , updated on every single postback (because it's still present in view scoped bean). api documentation , showcase example explicitly mentions should used view scoped beans.
you've there request scoped bean trashed , recreated on every single request, on postbacks same view. user property disappears , falls default null on every subsequent postback request.
there 2 ways fix it:
replace
<o:viewparam><f:viewparam>. call setter on every request, on postbacks.replace
@named @requestscoped@managedbean @viewscoped, way bean live long you're interacting same view. or if insist in using cdi, use@named @conversationscopedinstead, have manage begin , end of conversation yourself.
Comments
Post a Comment