【问题标题】:Clearing fields of dialog after use使用后清除对话框字段
【发布时间】:2014-08-27 17:18:26
【问题描述】:

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

但是当通过调用此对话框添加另一个用户时,以前的用户仍然存在于表单中

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

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

【问题讨论】:

    标签: jsf primefaces dialog


    【解决方案1】:

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

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

    然后您可以从 bean 的其他位置删除该行。

    【讨论】:

    • 几年前擎天柱说不更新对话框本身 - 它改变了吗?我想是的:)
    • @Jaqen:当您直接更新对话框本身时,旧 PF 版本中确实存在错误。您宁愿更新其内容。在 OP 的情况下,因此 update=":addForm"。但是,OP 使用的是 PF5,应该不会再有这个 bug 了。
    猜你喜欢
    • 2022-08-13
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多