0

I have a table with users. I can register a new user and delete any student from the table. But edit user is not working. I don't know why. Register a new user button on my jsf page looks like this:

<p:commandButton id="submit" value="Create Student" update=":dataTable" actionListener="#{studentManagedBean.createStudent()}" />

It is creating a new student in my database and updating my <p:dataTable> ..... </p:dataTable> with id "dataTable".

In the p:dataTable I have edit | delete commandLinks for each row. Delete commandLink looks like this.

<p:commandLink value="delete" update=":dataTable" action="#{studentManagedBean.deleteStudent(std)}"/>

It is deleting the selected student from my database and updating my <p:dataTable> aswell.

Edit commandLink looks like this.

<p:commandLink value="edit" update=":dataTable" action="#{studentManagedBean.editStudent(std)}" rendered="#{std.editable eq '0'}"/>            
<p:commandLink value="save" update=":dataTable" action="#{studentManagedBean.editStudent(std)}" rendered="#{std.editable eq '1'}"/>

And inside of each column in my dataTable looks like this.

<p:outputLabel value="#{std.firstName}" rendered="#{std.editable eq '0'}"/>
<p:inputText value="#{std.firstName}" rendered="#{std.editable eq '1'}"/>

As you can see, if editable (char in db) is '1' then it shows inputText field instead of outputLabel. Everything works fine up to this point. When I click on edit commandLink outputLabel fields hides and inputText fields shown and commandLink text changes from edit to save. But it does not update my std object with new values if I change old values in the row.

editStudent(std) method looks like this.

public void editStudent(Student std) {
   if(std.getEditable() == '0') {
     std.setEditable('1');
   } else {
     std.setEditable('0');
   }
   studentFacade.edit(std);
}

I change editable column manually before call studentFacade.edit(std) method. And it updates new editable values in DB fine. It just not getting newly edited values from inputText fields.

studentFacade.edit method looks like this.

public void edit(Student entity) {
   getEntityManager().merge(entity);
}

I can't grab new edited values into editStudent method from jsf page. What am I missing?

EDIT:

char worked without problem for my rendered elements. But I changed char to string and replaced old rendered with this one. rendered="#{std.editable eq '0'.charAt(0)}" Same result. Take a look these 2 photos.

when rendered is "0" and when rendered is "1"

I can't remove rendered in this case. Otherwise table view will not look the same. Is there a better solution?

EDIT 2:

1) I use MySQL and it is not support BOOLEAN as true or false but 0 or non 0 values. So, I can't use rendered="#{std.editable}" can I?

2) I have no <h:form> tag in datatable. Complete xhtml page looks like this:

<p:layout id="layout" styleClass="reg-student-layout">
            <p:layoutUnit id="layoutRegister" position="west" resizable="false" size="225">
                <h:form id="registerForm">
                    <h3>Registrera ny student</h3>

                    <h:outputText value="Förnamn"/><h:message for="first_name"/>
                    <br><p:inputText id="first_name" value="#{studentManagedBean.student.firstName}"/></br>

                    <h:outputText value="Efternamn"/><h:message for="last_name"/>
                    <br><p:inputText id="last_name" value="#{studentManagedBean.student.lastName}"/></br>

                    <h:outputText value="Telefon"/><h:message for="tel_number"/>
                    <br><p:inputText id="tel_number" value="#{studentManagedBean.student.telefon}"/></br>

                    <h:outputText value="Adress"/><h:message for="address"/>
                    <br><p:inputText id="address" value="#{studentManagedBean.student.address}"/></br>

                    <h:outputText value="Email"/><h:message for="email"/>
                    <br><p:inputText id="email" value="#{studentManagedBean.student.email}"/></br>

                    <h:outputText value="Start Date"/><h:message for="start_date"/><br/>
                    <p:calendar id="start_date" value="#{studentManagedBean.student.startDate}" pattern="dd-MM-yyyy" /><br/>

                    <br><p:commandButton id="submit" value="Skapa Student" update="registerForm, :dataTable"
                                         actionListener="#{studentManagedBean.createStudent()}" /></br>
                </h:form>
            </p:layoutUnit>

            <!-- TABLE VIEW -->
            <p:layoutUnit id="layoutTable" position="center">
                <p:dataTable id="dataTable" value="#{studentManagedBean.getStudents()}" var="std">

                    <p:column>
                        <f:facet name="header">Förnamn</f:facet>
                        <p:outputLabel value="#{std.firstName}" rendered="#{std.editable eq '0'.charAt(0)}"/>
                        <p:inputText value="#{std.firstName}" rendered="#{std.editable eq '1'.charAt(0)}"/>
                    </p:column>

                    <p:column>
                        <f:facet name="header">Efternamn</f:facet>
                        <p:outputLabel value="#{std.lastName}" rendered="#{std.editable eq '0'.charAt(0)}"/>
                        <p:inputText value="#{std.lastName}" rendered="#{std.editable eq '1'.charAt(0)}"/>
                    </p:column>

                    <p:column>
                        <f:facet name="header">Telefon</f:facet>
                        <p:outputLabel value="#{std.telefon}" rendered="#{std.editable eq '0'.charAt(0)}"/>
                        <p:inputText value="#{std.telefon}" rendered="#{std.editable eq '1'.charAt(0)}"/>
                    </p:column>

                    <p:column>
                        <f:facet name="header">Adress</f:facet>
                        <p:outputLabel value="#{std.address}" rendered="#{std.editable eq '0'.charAt(0)}"/>
                        <p:inputText value="#{std.address}" rendered="#{std.editable eq '1'.charAt(0)}"/>
                    </p:column>

                    <p:column>
                        <f:facet name="header">Email</f:facet>
                        <p:outputLabel value="#{std.email}" rendered="#{std.editable eq '0'.charAt(0)}"/>
                        <p:inputText value="#{std.email}" rendered="#{std.editable eq '1'.charAt(0)}"/>
                    </p:column>

                    <p:column>
                        <f:facet name="header">Registrerad</f:facet>
                        <p:outputLabel value="#{std.startDate}" rendered="#{std.editable eq '0'.charAt(0)}">
                            <f:convertDateTime pattern="dd-MM-yyyy"/>
                        </p:outputLabel>

                        <p:inputText value="#{std.startDate}" rendered="#{std.editable eq '1'.charAt(0)}">
                            <f:convertDateTime pattern="dd-MM-yyyy"/>
                        </p:inputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">Edit</f:facet>
                        <p:commandLink value="edit" update=":dataTable" action="#{studentManagedBean.editStudent(std)}" rendered="#{std.editable eq '0'.charAt(0)}"/>

                        <p:commandLink style="color: royalblue" value="save" update=":dataTable" action="#{studentManagedBean.editStudent(std)}" rendered="#{std.editable eq '1'.charAt(0)}"/>
                    </p:column>

                    <p:column>
                        <f:facet name="header">Ta bort</f:facet>
                        <p:commandLink value="delete" update=":dataTable" action="#{studentManagedBean.deleteStudent(std)}"/>
                    </p:column>
                </p:dataTable>
            </p:layoutUnit>
        </p:layout>

If I try to use <h:form> tag inside <p:layoutUnit position="center">, then I get this error. Cannot find component with expression ":dataTable" referenced from "registerForm:submit". And I tried all combinations in my register button's update attr. without any success. For ex: update="registerForm, :dataTableId", update="registerForm, :formId:dataTableId" (when I used form), update="registerForm, :layoutUnitId:formId:dataTableId".

3) I printed out and I get all field values from existing object. Nothing wrong there.

oxyt
  • 1,816
  • 3
  • 23
  • 33
  • To exclude one and other, remove `rendered` and retry. EL doesn't like `char` this way and that should be done differently (normally, with an enum, or in this case, a simple `boolean` as there are obviously no other states). Possible duplicate: http://stackoverflow.com/questions/14454261/rendered-attribute-not-working-on-a-character-property/ – BalusC Sep 19 '15 at 20:01
  • @BalusC thank you for your answer. I checked out the link you provided but it didn't help me. I added two screenshots to show you my problem. – oxyt Sep 19 '15 at 21:10
  • You do not need to permanently remove `rendered`. Just only once for testing so that you can exclude its logic from being the cause of the problem you're facing. Do so and if it works, then we know that it's indeed the cause of this all, then we know where to look in order to solve the problem. One of ways to naildown the cause of a problem is simply elimination. This is best to be done by simply creating a MCVE as instructed in http://stackoverflow.com/tags/jsf/info Otherwise you will only get blind/educated guesses as answers instead of one exactly explaining and solving your real problem. – BalusC Sep 20 '15 at 09:05
  • @BalusC I did as you instructed. Removed rendered and tried but it is still not working. I found another question [here](http://stackoverflow.com/questions/12239165/jsf-primefaces-issue-in-pages-with-a-layout) which you answered. I followed your answer but that's not working neither. – oxyt Sep 20 '15 at 12:11
  • Without a MCVE it's indeed only guessing. – BalusC Sep 20 '15 at 12:14

1 Answers1

0

I found my problem. In my editStudent method I used global student instance instead of the instance (std) passed into method and it worked just fine.

public void editStudent(Student std) {
   if(std.getEditable() == '0') {
     std.setEditable('1');
   } else {
     std.setEditable('0');
   }
   studentFacade.edit(student);
}
oxyt
  • 1,816
  • 3
  • 23
  • 33