2014-08-27 32 views
1

保存學生對象後,我使用 清除當前學生對象new Student();並關閉彈出的對話框使用後清除對話框的字段

但是當使用通過調用該對話框中添加其他用戶,以前的用戶挖牆角

如何清除其已保存

使用primefaces凌晨5點的學生對象, JSF 2

<!-- this is a pop dialog to add student . Starting --> 
    <p:dialog header="#{msgs.addStudent}" 
     widgetVar="addStudentDialog" id="addStudent" modal="true" 
     resizable="true"> 
     <h:form id="addForm"> 
      <p:panel id="addPanel" style="margin-bottom:10px;"> 
       <p:messages id="errorMessages" showDetail="true" /> 
       <h:panelGrid columns="3"> 

        <h:outputLabel for="newFirstName" value="#{msgs.firstName}: *" /> 
        <p:inputText id="newFirstName" 
         value="#{studentBean.newStudent.firstName}" required="true" 
         label="newFirstName"> 
         <f:validateLength minimum="10" maximum="20" /> 
        </p:inputText> 
        <p:message for="newStudent" /> 

        <h:outputLabel for="newLastName" value="#{msgs.lastName}: *" /> 
        <p:inputText id="newLastName" 
         value="#{studentBean.newStudent.lastName}" required="true" 
         label="newLastName"> 
         <f:validateLength minimum="10" maximum="20" /> 
        </p:inputText> 
        <p:message for="newStudent" /> 

        <h:outputLabel for="newAge" value="#{msgs.age}: *" /> 
        <p:inputText id="newAge" 
         value="#{studentBean.newStudent.age}" required="true" 
         label="newAge"> 
         <f:validateLongRange minimum="18" maximum="30" /> 
        </p:inputText> 
        <p:message for="newAge" /> 


        <f:facet name="footer"> 
         <p:commandButton value="Submit" 
          update=":studentTableForm:studentTable,errorMessages" 
          rendered="true" action="#{studentBean.createStudent()}" /> 
        </f:facet> 
       </h:panelGrid> 

      </p:panel> 

     </h:form> 
    </p:dialog> 
    <!-- this is a pop dialog to add student . Ending --> 

託管豆:

/** 
* 
*/ 
package com.student.beans; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import javax.inject.Named; 
import javax.enterprise.context.SessionScoped; 
import org.hibernate.Session; 
import org.primefaces.context.RequestContext; 

import com.student.entity; 
import com.student.hibernate.*; 

/** 
* @author techbrainless 
* 
*/ 
@SuppressWarnings("serial") 
@Named(value = "studentBean") 
@SessionScoped 
public class StudentBean implements Serializable { 

    private List<Student> students; 
    private Student newStudent = new Student(); 


    public StudentBean() { 
     if (this.students == null) 
      this.students = new ArrayList<>(); 
    } 

    public List<Student> getStudents() { 
     return students; 
    } 

    public void setStudents(List<Student> students) { 
     this.students = students; 
    } 


    /** to create Student **/ 
    public void createStudent() { 

     Session session = HibernateUtil.getSessionFactory().openSession(); 

     session.beginTransaction(); 
     session.save(this.newStudent); 

     session.getTransaction().commit(); 
     session.close(); 

     this.students.add(this.newStudent); 

     /** to clear the field */ 
     this.newStudent = new Student(); 
     RequestContext.getCurrentInstance().execute("PF('addStudentDialog').hide()"); 
    } 

    public String openPage() { 
     Session session = HibernateUtil.getSessionFactory().openSession(); 
     this.students = session.createQuery("from Student").list(); 
     session.close(); 
     return "/main/studentList.xhtml"; 
    } 


    /** 
    * @return the newStudent 
    */ 
    public Student getNewStudent() { 
     return newStudent; 
    } 

    /** 
    * @param newStudent the newStudent to set 
    */ 
    public void setNewStudent(Student newStudent) { 
     this.newStudent = newStudent; 
    } 
} 

回答

2

在打開之前更新對話框的內容。由於您沒有執行重定向/刷新,它仍然與您正在使用的視圖狀態完全相同。

<p:commandButton ... update=":addStudent" oncomplete="PF('addStudentDialog').show()" /> 

此外,您可能需要將new Student()作業移動到該命令按鈕。例如。

<p:commandButton ... action="#{bean.addStudent}" update=":addStudent" oncomplete="PF('addStudentDialog').show()" /> 

隨着

public void addStudent() { 
    student = new Student(); 
} 

然後你可以從你的豆別處刪除了這一行。

+0

幾年前,Optimus說不更新對話框本身 - 是否改變了?我猜是這樣:) – 2014-08-27 18:51:03

+0

@Jaqen:當你直接更新對話框本身時,舊的PF版本確實存在一個錯誤。您寧願更新其內容。在OP的情況下,因此'update =「:addForm」'。但是,OP使用PF5,它不應該有這個錯誤了。 – BalusC 2014-08-27 18:52:58

+0

感謝BalusC。這樣可行。我感激你。 – techbrainless 2014-08-28 18:17:51