jsf 2 - update component in another xhtml form from Custom layout p:selectOneRadio not working -
my xhtml split in menu area (defaultmenu.xhtml) , content area (defaultcontent.xhtml).
the code defaultmenu.xhtml is:
<h:form id="defaultmenuform"> <p:outputpanel id="menupanel" class="contain auto-fixed-center"> <p:panel id="pmenu" visible="#{phcontroller.user.menuvisible}"> <table id="stutable"> <tr> <td width="15%"> <p:outputlabel id="stuname" value="#{phcontroller.phbean.studentname}" /> </td> <td> <p:tabmenu activeindex="#{param.selectedtab}"> <p:menuitem value="home" outcome="phome" icon="ui-icon-star"> <f:param name="selectedtab" value="0" /> </p:menuitem> <p:menuitem value="bank" outcome="bhome" icon="ui-icon-person"> <f:param name="selectedtab" value="1" /> </p:menuitem> </p:tabmenu> </td> </tr> </table> </p:panel> </p:outputpanel> </h:form>
the defaultcontent.xhtml displays ph.xhtml content (as part of functional navigation) , code is:
<ui:define name="content"> <f:event listener="#{phcontroller.readpeople}" type="prerenderview"> </f:event> <h:form id="form"> <p:selectoneradio id="selstud" value="#{phcontroller.phbean.ssseq}" layout="custom"> <p:ajax update=":defaultmenuform:parentmenupanel :defaultmenuform:stuname" listener="#{phcontroller.onchangeperson}"/> <f:selectitems value="#{phcontroller.selectstudents}" /> </p:selectoneradio> <div style="width: 300px; float:left;"> <p:datagrid var="studentlist" value="#{phcontroller.liststudent}" columns="1" rowindexvar="stuindex"> <p:panel header="" style="text-align:left"> <h:panelgrid columns="1" style="width:100%"> <h:outputtext value="#{studentlist.studentname}" /> <p:radiobutton for=":form:selstud" itemindex="#{stuindex}"/> select </h:panelgrid> </p:panel> </p:datagrid> </div> </h:form> </ui:define>
the code backing bean is:
map<string, object> studentparam = new hashmap<>(); studentparam.put("studentseq", phbean.getssseq()); ls = getbasedaoservice().readstudent("readstudent", studentparam); phbean.setstudentname(ls.get(0).getstudentfirstname() + " " + ls.get(0).getstudentlastname());
as can see, calling onchangestu method display selected student name in defaultmenu.xhtml. using custom layout p:selectoneradio in ph.xhtml , onclick trying update p:outputlabel in defaultmenu.xhtml.
the backing bean method gets invoked , value set in variable phcontroller.phbean.studentname, update not working. checked using view source , id “:defaultmenuform:stuname”, tried updating menu panel ":defaultmenuform:menupanel”, none of works.
not sure how resolve this. please suggest.
including structure of .xhtmls
<h:body id="entirepagebody"> <div id="page"> <ui:insert name="header" > <ui:include src="/template/defaultheader.xhtml" /> </ui:insert> <ui:insert name="menu" > <ui:include src="/template/defaultmenu.xhtml" /> </ui:insert> <div id="content_div" class="auto-fixed-center"> <div id="content_div_padding" class="content-block"> <ui:insert name="content" > <ui:include src="/template/defaultcontent.xhtml" /> <ui:debug hotkey="z" /> </ui:insert> </div> </div> <ui:insert name="footer" > <ui:include src="/template/defaultfooter.xhtml" /> </ui:insert> </div> </h:body>
phcontroller.java:
public class phcontroller extends basecontroller implements serializable {
private list<stud> liststudent; private list selectstudents; selectitem option; private phbean phbean; private boolean menuvisible; int counter = 0; public phcontroller() { phbean = new phbean(); } public void readpeople() { liststudent = new arraylist<stud>(); liststudent.add(new stud(1, "john miller")); liststudent.add(new stud(2, "scott jackson")); selectstudents = new arraylist(); option = new selectitem(liststudent.get(0).getstudentseq(), "select"); selectstudents.add(option); option = new selectitem(liststudent.get(1).getstudentseq(), "select"); selectstudents.add(option); phbean.setssseq(string.valueof(1)); phbean.setselectedname(liststudent.get(0).getstudentname()); menuvisible = true; } public void onchangeperson() { phbean.setselectedname(liststudent.get(1).getstudentname()); } // getters , setters
}
phbean.java:
public class phbean implements serializable {
private string ssseq; private string studname; // used display name in menu bar. private string selectedname; public phbean() { } // getters , setters
}
i'd in p:ajax
in defaultcontent.xhtml
list of components updated should separated spaces only, no commas - try changing this:
update=":defaultmenuform:menupanel, :defaultmenuform:stuname"
to this:
update=":defaultmenuform:menupanel :defaultmenuform:stuname"
update
i played bit more , may have found clue - please add following code defaultmenuform
:
<p:messages autoupdate="true" showdetail="true" />
this should tracking reason failed validation (in case failing validation root cause - said, have rather limited possibility reproduce issue).
anyway, when selected item in p:selectoneradio, error message appeared:
conversion error setting value 'test001.student@4110c95c' 'null converter'.
and root cause on row:
<p:selectoneradio id="selstud" value="#{phcontroller.phbean.ssseq}" layout="custom">
p:selectoneradio
expects string
passed value
- , ssseq
of different type. try change way value
populated ensure string
- maybe different attribute of phbean
or brand new string
one.
note: if doesn't help, maybe update question simplified example of how phcontroller
, phbean
if test it.
update #2
so have explained there problem want call phcontroller.readpeople
every time page loaded/refreshed, instead gets loaded each , every ajax request, overwriting values.
in phcontroller
(it bean, right? session scoped?) add (omitted null
checks sake of readability):
public void readpeopleonget() { facescontext fc = facescontext.getcurrentinstance(); externalcontext ec = fc.getexternalcontext(); httpservletrequest req = (httpservletrequest) ec.getrequest(); string reqmethod = req.getmethod(); if ("get".equals(reqmethod)) { readpeople(); } }
with above method keep part of defaultcontext.xhtml
in place, provided called (i assume so), listener method changed:
<f:event listener="#{phcontroller.readpeopleonget}" type="prerenderview"> </f:event>
the method readpeopleonget
still called every request page, since ajax requests post
, call readpeople
when page loaded or refreshed whole.
this may not "perfectly clean" solution, seemed work when tested it.
update #3
well, since use primefaces, possible identify ajax call way:
public void readpeopleonget() { requestcontext rc = requestcontext.getcurrentinstance(); if (!rc.isajaxrequest()) { readpeople(); } }
but if got point latest comments, want run readpeople
when page loaded first time - following part better that.
you didn't answer if phcontroller
bean, assume (there no annotations visible code posted). may try making @sessionscoped
:
@managedbean @sessionscoped public class phcontroller extends basecontroller implements serializable { // rest of phcontroller goes here...
then add phcontroller
:
@postconstruct private void init() { readpeople(); }
the annotation @postconstruct
ensures method init
called once after bean created. can continue calling method readpeople
other places necessary, while removing both <f:event ... />
, readpeopleonget
these no longer needed.
Comments
Post a Comment