jsf 2 - Populate user form (edit row) with user-specific data in JSF Managed Bean -
i'm using jsf (primefaces) in liferay , have problems.
i have list of people (datatable) edit button every row in datatable. users have property differ 1 row others, e.g. football, basketball, tennis, , every user have of these categories. depending on properties, there different page/form needs filled. so, have 1 managedbean (pendingrequest.java) acts dispatcher/switcher recieves data person in row , switches user appropriate form. form has backing bean (e.g. page signfootballer.xhtml has bean footballer.java have getters , setters fields in page signfootballer.xhtml). so, user clicks on edit button row in table, method dispatchrequest starts running , checks category (football,...) , depending on category , initialize new object of class footballer , sets properties fill form (e.g. setname("john"), etc... because "edit user" functionality of application).
public string dispatchrequest(person p) { if (p.getcategory.equals("tennis")) { return "not_ok"; } else if (p.getcategory().equals("basketball")) { return "not_ok"; } else if (p.getcategory().equals("football")) { footballer f = new footballer(); f.setname("john"); f.setlastname("doe"); return "ok"; } else { return "not_ok"; } }
after that, i'm switching form signfootballer.xhtml via faces-config.xml. , goes correctly, problem form empty.
i'm aware isn't correct approach need advice, can suggest me best way how update row datatable having in mind need differ rows category , open appropriate page , corresponding backing bean fill form. because i'm not sure possible using dispatcher class.
edit: @balusc, not real case, showing example understand problem. have 3 different form, depending on cases footballplayer, basketballplayer , tennisplayer.
signfootballer.xhtml
<h:form enctype="multipart/form-data" id="form"> <div id="container"> <div class="divs"> <p style="margin-left: 22%; width:60%; font-size: 20px; font-weight: bold">sign contract</p> <table> <tr> <td> <p:outputlabel for="name" value="first name:*" styleclass="f_text" /> <p:inputtext id="name" value="#{footballer.name}" required="true" /> </td> <td> <p:outputlabel for="surname" value="last name:" styleclass="f_text" /> <p:inputtext id="surname" value="#{footballer.surname}" required="true" /> </td> </tr> </table> </div> <div class="submit"> <p:commandbutton value="send" id="submit" update="err_msg1 err_msg2 err_msg3" action="#{footballer.submitform}" styleclass="button" /> </div> </div> </h:form>
footballer.java (additional variables , method deleted)
@managedbean @viewscoped public class footballer { private string name; private string surname; public void submitform() { // validate data , save in database } // getters , setters
pendingrequest.xhtml (it list of request hasn't been confirmed yet - database)
<h:form id="form"> <h:panelgroup id="table-wrapper"> <p:datatable id="dt" var="req" value="#{pendingrequest.requestlist}"> <f:facet name="header"> pregled prijava </f:facet> <p:column headertext="id"> <h:outputtext value="#{req.id}" /> </p:column> <p:column headertext="category"> <h:outputtext value="#{req.category}" /> </p:column> <p:column headertext="name"> <h:outputtext value="#{req.name}" /> </p:column> <p:column headertext="prezime"> <h:outputtext value="#{req.surname}" /> </p:column> <p:column headertext="delete/edit" width="10%" style="text-align:center" > <p:commandlink immediate="true" update=":form:table-wrapper" action="#{pendingrequest.deleterecord(p)}"/> <p:commandlink immediate="true" action="#{pendingrequest.dispatchrequest(p)}" style="margin-left:5px;"/> </p:column> </p:datatable> </h:panelgroup> </h:form>
pendingrequests.java (class method run after user choose edit request list of pending requests)
@managedbean @viewscoped public class pendingrequest { public list<requests> requestlist = new arraylist<requests>(); // "requests" entity class generated database public list<requests> getrequestlist() { sessionfactory sessionfactory = hibernateutil.getsessionfactory(); //save example - without transaction session session = sessionfactory.opensession(); session.begintransaction(); list<requests> templist = session.createcriteria(requests.class).list(); session.gettransaction().commit(); return templist; } public void setrequestlist(list<requests> requestlist) { this.requestlist = requestlist; } public void deleterecord(requests p) { session session = hibernateutil.getsessionfactory().opensession(); try { session.begintransaction(); session.delete(p); session.gettransaction().commit(); } catch (hibernateexception ex) { ex.printstacktrace(); session.gettransaction().rollback(); } } public void dispatchrequest(requests p) { if (p.getcategory().equals("football")) { //transfer id of request footballer class populate fields in signfootball.xhtml } else if (p.getcategory.equals("basketball")) { //transfer id of request basketballplayer class populate fields in signbasketballplayer.xhtml } else if (p.getcategory().equals("tennis")) { //transfer id of request tennisplayer class populate fields in signtennisplayer.xhtml } else { system.out.println("other..."); } }
method dispatchrequests inspecting category (football, basketball, tennis) of request , depending on it, needs open appropriate form , fill values. same form new request creation, filled data database.
so, if user clicks request footballer needs pass request id in footballer class constructor? because fields need set before page rendered?
that part of story isn't clear me. i'm new jsf , appreciate if give me suggestions how edit funcionality?
note: idea.
it sufficient create inline datatable editor. on edit button call bean actionlistener extract user object based on datatable index. check category , populate related class object. object properties fill fields. render button based on property identify action. call bean actionlistener on update / save button , forward data service layer. on successful operation show relevant message.
Comments
Post a Comment