使用后清除对话框的字段

时间:2014-08-27 17:18:27

标签: jsf primefaces dialog

保存学生对象后,我正在使用清除当前学生对象 新生();并关闭弹出对话框

但是当通过调用此对话框添加其他用户时,前一个用户将保留在表单

如何清除已保存的学生对象

我正在使用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 -->

管理bean:

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

1 个答案:

答案 0 :(得分:2)

打开前更新对话框的内容。由于您没有执行重定向/刷新,因此它仍然与您正在使用的视图状态完全相同。

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

此外,您可能希望将new Student()作业移动到该命令按钮。 E.g。

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

使用

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

然后你可以从bean的其他地方删除该行。