1

I am getting the mysql "login" table data on a xhtml page, and then trying to edit a single row and then update it. The problem is when i click the edit column for a single row, all the rows become editable. My xhtml page code is:

        <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<title>Users</title>
 <h:outputStylesheet library="css" name="style.css" />
</h:head>
<h:body>

<h1>Users</h1>

        <h:form>
      <h:dataTable value="#{loginBean.list}"  var="list" border="1">
         <h:column>                 
            <f:facet name="header">Name</f:facet>  
            <h:inputText value="#{list.UName}" size="10" rendered="#{list.isEditable}" />               
            <h:outputText value="#{list.UName}" rendered="#{not list.isEditable}" />
         </h:column>
         <h:column>
            <f:facet name="header">Email</f:facet>
            <h:inputText value="#{list.emailAdd}" size="10" rendered="#{list.isEditable}" />                
            <h:outputText value="#{list.emailAdd}" rendered="#{not list.isEditable}" />
         </h:column>
          <h:column>
            <f:facet name="header">Password</f:facet>
            <h:inputText value="#{list.pword}" size="10" rendered="#{list.isEditable}" />               
            <h:outputText value="#{list.pword}" rendered="#{not list.isEditable}" />
         </h:column>

         <h:column>
            <f:facet name="header">Edit</f:facet>
            <h:commandLink value="Edit" action="#{loginBean.editable(list)}"/>
         </h:column>

         <h:column>
            <f:facet name="header">Update</f:facet>
            <h:commandLink value="Update" action="#{loginBean.updateAction()}"  />
         </h:column>

       <h:column>
            <f:facet name="header">Delete</f:facet>
            <h:commandLink value="Delete" action="#{loginBean.deleteAction(list)}" />
       </h:column>


      </h:dataTable>
   </h:form>


</h:body>
</html>

and the loginBean class code is:

   package com;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityManager;

import entity.Login;
import session.LoginManagerRemote;
/**
 * @author asma.fardoos
 *
 */
@ManagedBean(name = "loginBean" , eager=true)
@ViewScoped
public class LoginBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private String emailid;
    private String password;
    boolean isEditable;


    @EJB
    LoginManagerRemote loginmanager;
    EntityManager em;

    List<Login> list=null;

@ManagedProperty(value="#{navigationBean}")
private NavigationBean navigationBean;

public LoginBean (){


}

    public String dologin()
    {
        /**String username = "madiha";
        String pass = "madiha";
        if (username.equals(emailid) && pass.equals(password)) {
            //JOptionPane.showInputDialog("Eggs are not supposed to be green.");
            // System.out.print("You are logged IN");
            return navigationBean.toWelcome();
        }

        return navigationBean.tofailure();
        */
            System.out.println(">>>> within actionLogin method <<<<");
            if (loginmanager.isUserValid(this.emailid, this.password)){ 
                return navigationBean.toWelcome();
            }
            else
                return navigationBean.tofailure();      
        }

    public List<Login> getList() {
        System.out.println(">>>> In List  method <<<<");
        list = new  ArrayList<Login>();
        list = loginmanager.findAll();

        return list;
    }
    public void setList(List<Login> list) {
        this.list = list;
    }

    public String deleteAction(Login del) {
        System.out.println(">>>> I am in delete method <<<<"); 
        loginmanager.delete(del);
        return "";
    }



public String editable(Login lis)
    {System.out.println(">>>> I am in editable method <<<<");
            lis.setIsEditable(true);
            System.out.println(">>>> I have set editable to true <<<<"); 
        return null;
    }

 public String editAction(Login lis) {
//   LoginManager log = new LoginManager();
    //Login test = log.getRecordByID(lis.getId());
     loginmanager.Edit(lis);
    System.out.println(">>>> I am in editAction method <<<<"); 
    //setIsEditable(true);  
     return "";
 }


 public void updateAction() {

  //  setIsEditable(false); 
    // Query q = em.createQuery("UPDATE Login SET UName='foziiii' ,emailAdd= 'foziiiii',pword='fozi' WHERE id=21");
   //  em.persist();

 }



    public String getEmailid() {
        return emailid;
    }

    public void setEmailid(String emailid) {
        this.emailid = emailid;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;

}
    public void setNavigationBean(NavigationBean navigationBean) {
        this.navigationBean = navigationBean;
}

}

and the login entity class, edit method is:

   @Entity
public class Login implements Serializable {
    private static final long serialVersionUID = 1L;


    @Id
    private int id;

    private String emailAdd;

    private String pword;

    private String UName;

    public Login() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEmailAdd() {
        return this.emailAdd;
    }

    public void setEmailAdd(String emailAdd) {
        this.emailAdd = emailAdd;
    }

    public String getPword() {
        return this.pword;
    }

    public void setPword(String pword) {
        this.pword = pword;
    }

    public String getUName() {
        return this.UName;
    }

    public void setUName(String UName) {
        this.UName = UName;
    }
    @Transient
    private boolean isEditable;

    public boolean getIsEditable() {
        return isEditable;
    }

    public void setIsEditable(boolean isEditable) {
        this.isEditable = isEditable;

    }
}
Hanya Idrees
  • 453
  • 1
  • 8
  • 24

1 Answers1

1

The problem is you are using the same boolean state for all the rows, you need a separate state for each row.

So replace

<h:column>                 
<f:facet name="header">Name</f:facet>  
<h:inputText value="#{list.UName}" size="10" rendered="#{loginBean.isEditable}"/>              
<h:outputText value="#{list.UName}" rendered="#{not loginBean.isEditable}" />
</h:column>
<h:column>
<f:facet name="header">Email</f:facet>
<h:inputText value="#{list.emailAdd}" size="10" rendered="#{loginBean.isEditable}"/>               
<h:outputText value="#{list.emailAdd}" rendered="#{not loginBean.isEditable}" />
</h:column>
<h:column>
<f:facet name="header">Password</f:facet>
<h:inputText value="#{list.pword}" size="10" rendered="#{loginBean.isEditable}"/>              
<h:outputText value="#{list.pword}" rendered="#{not loginBean.isEditable}" />
</h:column>

by

<h:column>                 
<f:facet name="header">Name</f:facet>  
<h:inputText value="#{list.UName}" size="10" rendered="#{list.isEditable}"/>              
<h:outputText value="#{list.UName}" rendered="#{not list.isEditable}" />
</h:column>
<h:column>
<f:facet name="header">Email</f:facet>
<h:inputText value="#{list.emailAdd}" size="10" rendered="#{list.isEditable}"/>               
<h:outputText value="#{list.emailAdd}" rendered="#{not list.isEditable}" />
</h:column>
<h:column>
<f:facet name="header">Password</f:facet>
<h:inputText value="#{list.pword}" size="10" rendered="#{list.isEditable}"/>              
<h:outputText value="#{list.pword}" rendered="#{not list.isEditable}" />
</h:column>

And add

@Transient
private boolean isEditable;

//+ getter and setter

into your Login entity.

Edit : As per the comment section, how to update the view after the action ?

Using Ajax , your link becomes

<h:commandLink value="Edit" action="#{loginBean.editable(list)}">
<f:ajax render="yourDataTable"/>
</h:commandLink>

Also see Difference between returning null and “” from a JSF action

Community
  • 1
  • 1
  • @BalucC I have made the changes, and edited the question . Please review. Now when i click on Edit link, Nothing happens, no field is editable – Hanya Idrees Nov 24 '14 at 10:28
  • You added `rowStatePreserved="true"`.. why ? And please modify your question so we can see the scopes, etc. I'm not BalucC btw.. –  Nov 24 '14 at 10:31
  • sorry, I have made the changes, please look – Hanya Idrees Nov 24 '14 at 10:47
  • Please review, There are no changes in the result, Still nothing happens on clicking edit – Hanya Idrees Nov 24 '14 at 11:11
  • Sivagrunathan yes it is called, i have checked it. and it calls the login entity setiseditable also. But they do nothing, i dnt know why – Hanya Idrees Nov 24 '14 at 13:31
  • Well, try to return an empty string and not `null`, and or update the page. You should use Ajax for that purpose. –  Nov 24 '14 at 13:35
  • Are you updating the page after ? –  Nov 26 '14 at 08:00
  • When you update the values in the backing bean, you need to refresh the view on the client side to see the changes, for that you can use an ajax request or an non-ajax request. Ajax request is recommended as it is usually lighter, I will edit my answer with an example. –  Nov 27 '14 at 18:02