大家好我是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>
它在哪裏提到,你必須強制使用一個會話範圍的託管bean對於這種在JSF 2.x.x東西呢? – Tiny
@Tiny 我看到一個關於這個的例子,它使用了會話作用域。只有一個區別,我使用hibernate從數據庫獲取信息,該示例使用列表記錄名稱,surnama。該示例沒有使用數據庫的連接。那麼我應該用什麼樣的範圍來思考? – umitkilic
在執行基本的CRUD操作時,視圖範圍的bean應該足夠多次。 – Tiny