2015-09-08 97 views
0

大家好我是JSF-Hibernate的新手。我在我的數據庫中有一個名爲「Personels」的表格,我從那裏用一個函數獲取記錄並在「ListAllPersonels.xhtml」頁面顯示。我在每個姓氏旁邊都有一個鏈接,點擊它時,點擊的行應該轉到可編輯區域。我有布爾變量保存每個記錄的值。除了可編輯的區域鏈接外,其餘都是工作。當點擊它時,沒有任何變化。 「可編輯」的值不會從「false」變爲「真」。所以我的代碼在這裏,問題在哪裏?謝謝....在JSF中設置帶有鏈接的可編輯/不可編輯區域?

My ManagedBean - PersonelMB;

@ManagedBean 
@SessionScoped 
public class PersonelMB implements Serializable{ 

/** 
* Creates a new instance of PersonelMB 
*/ 
public PersonelMB() { 
} 
private int personelid; 
private String personelname,personelsurname; 

public int getPersonelid() { 
    return personelid; 
} 

public void setPersonelid(int personelid) { 
    this.personelid = personelid; 
} 

public String getPersonelname() { 
    return personelname; 
} 

public void setPersonelname(String personelname) { 
    this.personelname = personelname; 
} 

public String getPersonelsurname() { 
    return personelsurname; 
} 

public void setPersonelsurname(String personelsurname) { 
    this.personelsurname = personelsurname; 
} 



private NewHibernateUtil hu; 
private Session s; 
private Personels personel; 
private List<Personels> plist; 

public List<Personels> getAllPersonelsFromDatabase(){ 
    plist=new ArrayList<>(); 
    s=hu.getSessionFactory().openSession(); 
    s.beginTransaction(); 
    Query qu=s.createQuery("from Personels"); 
    plist=qu.list(); 
    s.getTransaction().commit(); 
    s.close(); 
    return plist; 
} 

public String addRecord(){ 
    Personels p=new Personels(); 
    p.setPersonelName(personelname); 
    p.setPersonelSurname(personelsurname); 
    s=hu.getSessionFactory().openSession(); 
    s.beginTransaction(); 
    s.save(p); 
    s.getTransaction().commit(); 
    s.close(); 
    p=null; 
    personelname=""; 
    personelsurname=""; 
    return "success"; 
} 

public String editRecord(Personels p){ 
    p.setEditable(true); 
    return null; 
} 

public String saveAlteration(Personels pr){ 
    for(Personels pers:mylist) 
     pers.setEditable(false); 

    s=hu.getSessionFactory().openSession(); 
    s.beginTransaction(); 
    s.update(pr); 
    s.getTransaction().commit(); 
    s.close(); 
    return null; 
} 
public String deleteRecord(Personels p){ 
    s=hu.getSessionFactory().openSession(); 
    s.beginTransaction(); 
    s.delete(p); 
    s.getTransaction().commit(); 
    s.close(); 
    return "deleted"; 
} 
} 

我的Java類 - Personels.java

public class Personels implements java.io.Serializable { 


private Integer personelId; 
private String personelName; 
private String personelSurname; 

public Personels() { 
} 

public Personels(String personelName, String personelSurname) { 
    this.personelName = personelName; 
    this.personelSurname = personelSurname; 
} 

public Integer getPersonelId() { 
    return this.personelId; 
} 

public void setPersonelId(Integer personelİd) { 
    this.personelId = personelİd; 
} 
public String getPersonelName() { 
    return this.personelName; 
} 

public void setPersonelName(String personelName) { 
    this.personelName = personelName; 
} 
public String getPersonelSurname() { 
    return this.personelSurname; 
} 

public void setPersonelSurname(String personelSurname) { 
    this.personelSurname = personelSurname; 
} 

boolean editable; 

public boolean isEditable() { 
    return editable; 
} 

public void setEditable(boolean editable) { 
    this.editable = editable; 
} 

} 

我的index.xhtml

<h:form> 
     <h:panelGrid columns="2"> 
      <f:facet name="Personel Register Form"/> 
      <h:outputLabel value="NAME: "/> 
      <h:inputText value="#{personelMB.personelname}"/> 

      <h:outputLabel value="SURNAME: "/> 
      <h:inputText value="#{personelMB.personelsurname}"/> 
      <h:commandButton value="Reset" type="reset"/> 
      <h:commandButton value="Save" type="submit" action="#{personelMB.addRecord()}"/> 
     </h:panelGrid> 
     <h:commandLink value="List All Personels" action="listallpersonels.xhtml"/> 
    </h:form> 

我listallpersonels.xhtml;

<h:form> 
    <h:dataTable value="#{personelMB.getAllPersonelsFromDatabase()}" var="p" > 
     <h:column> 
      <f:facet name="header">NAME</f:facet> 
      <h:inputText value="#{p.personelName}" rendered="#{p.editable}" size="15"/> 
      <h:outputText value="#{p.personelName}" rendered="#{not p.editable}"/> 

     </h:column> 

     <h:column> 
      <f:facet name="header">SURNAME</f:facet> 
      <h:outputText value="#{p.personelSurname}" rendered="#{not p.editable}"/> 
      <h:inputText value="#{p.personelSurname}" rendered="#{p.editable}" size="15" /> 
     </h:column> 

     <h:column> 
      <f:facet name="header">DELETE</f:facet> 
      <h:commandLink value="Delete" action="#{personelMB.deleteRecord(p)}"/> 
     </h:column> 

     <h:column> 
      <f:facet name="header">EDIT</f:facet> 
      <h:commandLink value="Edit" action="#{personelMB.editRecord(p)}" 
            rendered="#{not p.editable}"/> 

      <h:commandButton value="Save Alteration" action="#{personelMB.saveAlteration(p)}" 
            rendered="#{p.editable}" /> 
     </h:column> 

    </h:dataTable> 
</h:form> 
+0

它在哪裏提到,你必須強制使用一個會話範圍的託管bean對於這種在JSF 2.x.x東西呢? – Tiny

+0

@Tiny 我看到一個關於這個的例子,它使用了會話作用域。只有一個區別,我使用hibernate從數據庫獲取信息,該示例使用列表記錄名稱,surnama。該示例沒有使用數據庫的連接。那麼我應該用什麼樣的範圍來思考? – umitkilic

+0

在執行基本的CRUD操作時,視圖範圍的bean應該足夠多次。 – Tiny

回答

0

你應該看看primefaces,它有一些功能。你應該對你的問題找到一些答案。

http://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml

再嘗試這樣的事情

<p:dataTable value="#{personelMB.getAllPersonelsFromDatabase()}" var="p" editable="true" editMode="cell" widgetVar="cellP"> 
+0

感謝@DamienBoue,我會研究它,但我想學習如何解決沒有primeface。 :) – umitkilic

+0

我不知道如何做你想做的事,但我認爲有一種可能性是用一個鏈接讓你的包含一個監聽器女巫,將你的可編輯值更改爲true,然後更新你的視圖。 – DamienB