使用JSF编辑单行mysql

时间:2014-11-24 08:00:17

标签: java eclipse jsf java-ee

我在xhtml页面上获取mysql“login”表数据,然后尝试编辑单行然后更新它。问题是,当我单击一行的编辑列时,所有行都可以编辑。我的xhtml页面代码是:

        <?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>

,loginBean类代码为:

   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;
}

}

和登录实体类,编辑方法是:

   @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;

    }
}

1 个答案:

答案 0 :(得分:1)

问题是你对所有行使用相同的布尔状态,每行需要一个单独的状态。

所以替换

<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>

通过

<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>

并添加

@Transient
private boolean isEditable;

//+ getter and setter

进入Login实体。

编辑:根据评论部分,如何在操作后更新视图?

使用Ajax,您的链接变为

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

另见Difference between returning null and “” from a JSF action