0
votes

I am unable to update a <p:inputText> based on the value entered in another <p:inputText> using <p:ajax>. I am using JSF 2 with PrimeFaces 5. The second <p:inputText> is not updating, the listener associated with <p:ajax> is being called and i am getting the correct values in the listener but its not updated on the view.

the view code is:

<p:dialog id="newStdDlg" header="Add new Student" widgetVar="newStdDlg" modal="true">
    <h:panelGrid id="newStdDlgPanel" columns="2" cellpadding="5" style="width:100%;">
        <p:outputLabel value="First Name *" />
        <p:inputText id="studentfname" value="#{addStudentBean.student.firstName}">
            <p:ajax event="change" update="studentUsrname" listener="#{addStudentBean.firstNameChange}" />
        </p:inputText>

        <p:outputLabel value="Last Name *" />
        <p:inputText value="#{addStudentBean.student.lastName}"/>

        <p:outputLabel value="Father's Name *" />
        <p:inputText value="#{addStudentBean.student.fatherName}"/>

        <p:outputLabel id="uLbl" value="Username (System Generated) *" />
        <p:inputText id="studentUsrname" value="#{addStudentBean.student.user.username}" />

        <p:outputLabel value="This temporary password would be mailed to user: " />
        <p:outputLabel id="stdpassword" value="#{addStudentBean.student.user.password}"/>
    </h:panelGrid>

    <p:commandButton value="Create Student"
                     actionListener="#{addStudentBean.addNewStudentAction}"
                     style="margin-left:auto;margin-right:auto;display:block;"/>
</p:dialog>

and the listener of the session scoped managed bean is :

public void firstNameChange() {
    System.out.println("In AddStudentBean().firstNameChange()..........");
    System.out.println("The value of student.getFirstName: "+student.getFirstName());
    System.out.println("updating system generated username as: "+student.getFirstName()+String.valueOf(new UserDAO().getUserCount()+1));
    student.getUser().setUsername(student.getFirstName()+String.valueOf(new UserDAO().getUserCount()+1));
    student.getUser().setPassword(KaaloUtils.getPassword());
}
1
It used to be good practise to have a h:form inside the dialog, and place the dialog below other forms in the page. You could try it. Otherwise do you have any console errors (in the browser)?Jaqen H'ghar
@JaqenH'ghar: Thankyou so much, it just worked the way you mentioned by the dialog having its own h:form. (Y)Hassan

1 Answers

0
votes

Like Jaqen mentioned in comments the comments use h:form inside dialog.

If you want to update the component from ManagedBean you can do that by using org.primefaces.RequestContext's update method.

RequestContext.getCurrentInstance().update("COMPONENT_ID_TO_UPDATE")

If you feel like this method is too Cohesive, you can update from Facelet only, make sure remember to not place p:dialog in a h:from instead use h:form inside p:dialog .