Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 为什么<;p:inputText>;不在bean中填充数据_Jsf - Fatal编程技术网

Jsf 为什么<;p:inputText>;不在bean中填充数据

Jsf 为什么<;p:inputText>;不在bean中填充数据,jsf,Jsf,我有一些JSF页面和一些ManageBean。在ManageBean中,我创建了一些odel文件,并尝试用JSF填充它。但它不会在中设置值。但当我使用现有的模型对象时,效果非常好 以下是我的JSF代码: <h:form> <c:set var="patient" value="#{manageBean.patient}" /> <p:panel id="panel" header="Patien

我有一些JSF页面和一些ManageBean。在ManageBean中,我创建了一些odel文件,并尝试用JSF填充它。但它不会在中设置值。但当我使用现有的模型对象时,效果非常好

以下是我的JSF代码:

 <h:form>           
            <c:set var="patient" value="#{manageBean.patient}" />
            <p:panel id="panel" header="Patient" style="margin-bottom:10px;">  
                <h:panelGrid columns="2">  
                    <h:outputLabel value="First  name" />  
                    <p:inputText id="firstName" required="true" value="#{patient.firstName}" />  

                    <h:outputLabel value="Family  name" />  
                    <p:inputText id="familyName" required="true" value="#{patient.familyName}" />  

                    <h:outputLabel value="Sex"/>
                    <p:selectOneMenu id="sex" rendered="true" value="#{patient.sex}">
                        <f:selectItem itemLabel="Male" itemValue="male" />  
                        <f:selectItem itemLabel="Female" itemValue="female" />  
                    </p:selectOneMenu>

                    <h:outputLabel value="Birthday date" />  
                    <p:calendar value="#{patient.birthdayDate}" mode="inline" id="birthdayDate"/>  

                    <h:outputLabel value="Nationality"/>
                    <p:selectOneMenu id="nationality" rendered="true" value="#{patient.nationality}">
                        <f:selectItem itemLabel="Russian" itemValue="russian" />  
                        <f:selectItem itemLabel="Ukranian" itemValue="ukranian" />  
                    </p:selectOneMenu>

                    <h:outputLabel value="Adress" />  
                    <p:inputText id="adress" required="true" value="#{patient.adress}" />  

                    <h:outputLabel value="Phone number" />  
                    <p:inputMask id="phoneNumber" required="true" value="#{patient.phoneNumber}" mask="(999) 999-9999"/>
                </h:panelGrid>  
            </p:panel> 
            <p:commandButton value="Save" action="#{manageBean.save}" />  
        </h:form>   
尝试更改:

<c:set var="patient" value="#{manageBean.patient}" />
然后对patient属性使用普通的getter和setter(无自定义代码)。

尝试更改:

<c:set var="patient" value="#{manageBean.patient}" />

然后对patient属性使用普通的getter和setter(无自定义代码)。

解决此问题的另一种方法是根本不使用任何参数,只将
UIInput
组件直接绑定到属性:


此外,遵循JSF最佳实践,您可以用两种方式重新定义托管bean(据我所知):

  • 处理ajax请求不需要使用
    @SessionScoped
    注释,这也意味着构造函数(以及
    @PostConstruct
    方法)在每个会话中只调用一次。对于这种情况,最好的选择是
    @ViewScoped
    注释。更多信息:

  • 在getter/setter方法中不能有任何业务逻辑,因为它将对JSF代码中的每个
    {managedBean.property}
    执行,更多信息:。知道了这一点,最好在bean构造函数或
    @PostConstruct
    方法中只加载一次会话数据

有了这些,您的托管bean应该如下所示:

@ManagedBean(name = "manageBean")
@ViewScoped
public class ManageBean implements Serializable {

    private Patient patient;
    private SessionFactory factory;

    public ManageBean() {
    }

    @PostConstruct
    public void init() {
        //it would be better to initialize everything here
        factory = SessionFactoryWrap.getInstance();
        patient = (Patient)FacesContext.getCurrentInstance().getExternalContext().
            getSessionMap().get("patient");
        if (patient == null) {
            patient = new Patient();
        }
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    public String save() {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.saveOrUpdate(patient);
            tx.commit();
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            ex.printStackTrace();
            //in my opinion, it would be better to show a descriptive message
            //instead of returning to the `go_home` view in case of errors.
        } finally {
            session.close();
        }
        //clumsy code line, no need to have it at all
        //patient = null;
        //Don't set the parameter to null, instead remove it from the session map.
        FacesContext.getCurrentInstance().getExternalContext().
            getSessionMap().remove("patient");
        return "go_home";

    }
}

解决此问题的另一种方法是根本不使用任何参数,直接将
UIInput
组件绑定到属性:


此外,遵循JSF最佳实践,您可以用两种方式重新定义托管bean(据我所知):

  • 处理ajax请求不需要使用
    @SessionScoped
    注释,这也意味着构造函数(以及
    @PostConstruct
    方法)在每个会话中只调用一次。对于这种情况,最好的选择是
    @ViewScoped
    注释。更多信息:

  • 在getter/setter方法中不能有任何业务逻辑,因为它将对JSF代码中的每个
    {managedBean.property}
    执行,更多信息:。知道了这一点,最好在bean构造函数或
    @PostConstruct
    方法中只加载一次会话数据

有了这些,您的托管bean应该如下所示:

@ManagedBean(name = "manageBean")
@ViewScoped
public class ManageBean implements Serializable {

    private Patient patient;
    private SessionFactory factory;

    public ManageBean() {
    }

    @PostConstruct
    public void init() {
        //it would be better to initialize everything here
        factory = SessionFactoryWrap.getInstance();
        patient = (Patient)FacesContext.getCurrentInstance().getExternalContext().
            getSessionMap().get("patient");
        if (patient == null) {
            patient = new Patient();
        }
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    public String save() {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.saveOrUpdate(patient);
            tx.commit();
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            ex.printStackTrace();
            //in my opinion, it would be better to show a descriptive message
            //instead of returning to the `go_home` view in case of errors.
        } finally {
            session.close();
        }
        //clumsy code line, no need to have it at all
        //patient = null;
        //Don't set the parameter to null, instead remove it from the session map.
        FacesContext.getCurrentInstance().getExternalContext().
            getSessionMap().remove("patient");
        return "go_home";

    }
}

此处需要解决的问题:
是不必要的。
#{managedBean.patient}
的存在意味着您可以像无JSTL一样访问它。2.除非您只是决定不给我们看,否则没有
患者的setter
。在没有setter的模型更新期间,不会将任何内容保存到支持bean。您在
getPatient()
中的整个方法都是错误和混乱的。您有
一个
pageScoped
car
patient
并且在您的后台bean中,您正在从会话中检索它??你们到底想在这里实现什么?我从另一个页面发送病人对象到这里,我从分离it工作中得到什么对象good@kolossus:不需要
患者的设置器。JSF/EL在
患者
自身中调用setter。例如,
managedBean.getPatient().setFirstName(firstName)
。问题确实出在getter中。每次它都会覆盖初始值,因此所有提交的值都会丢失。我发现这里有很多问题需要解决:
是不必要的。
#{managedBean.patient}
的存在意味着您可以像无JSTL一样访问它。2.除非您只是决定不给我们看,否则没有
患者的setter
。在没有setter的模型更新期间,不会将任何内容保存到支持bean。您在
getPatient()
中的整个方法都是错误和混乱的。您有
一个
pageScoped
car
patient
并且在您的后台bean中,您正在从会话中检索它??你们到底想在这里实现什么?我从另一个页面发送病人对象到这里,我从分离it工作中得到什么对象good@kolossus:不需要
患者的设置器。JSF/EL在
患者
自身中调用setter。例如,
managedBean.getPatient().setFirstName(firstName)
。问题确实出在getter中。每次都会覆盖初始值,因此所有提交的值都会丢失。我发现bug ty man非常感谢你的想法,从你的答案中获得了更大的部分。Aswesom想法非常感谢,从你的答案中获得了更大的部分。
@ManagedBean(name = "manageBean")
@ViewScoped
public class ManageBean implements Serializable {

    private Patient patient;
    private SessionFactory factory;

    public ManageBean() {
    }

    @PostConstruct
    public void init() {
        //it would be better to initialize everything here
        factory = SessionFactoryWrap.getInstance();
        patient = (Patient)FacesContext.getCurrentInstance().getExternalContext().
            getSessionMap().get("patient");
        if (patient == null) {
            patient = new Patient();
        }
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    public String save() {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.saveOrUpdate(patient);
            tx.commit();
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            ex.printStackTrace();
            //in my opinion, it would be better to show a descriptive message
            //instead of returning to the `go_home` view in case of errors.
        } finally {
            session.close();
        }
        //clumsy code line, no need to have it at all
        //patient = null;
        //Don't set the parameter to null, instead remove it from the session map.
        FacesContext.getCurrentInstance().getExternalContext().
            getSessionMap().remove("patient");
        return "go_home";

    }
}