Jsf 为什么<;p:inputText>;不在bean中填充数据
我有一些JSF页面和一些ManageBean。在ManageBean中,我创建了一些odel文件,并尝试用JSF填充它。但它不会在中设置值。但当我使用现有的模型对象时,效果非常好 以下是我的JSF代码: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
<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代码中的每个
执行,更多信息:。知道了这一点,最好在bean构造函数或{managedBean.property}
方法中只加载一次会话数据@PostConstruct
@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代码中的每个
执行,更多信息:。知道了这一点,最好在bean构造函数或{managedBean.property}
方法中只加载一次会话数据@PostConstruct
@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
carpatient
并且在您的后台bean中,您正在从会话中检索它??你们到底想在这里实现什么?我从另一个页面发送病人对象到这里,我从分离it工作中得到什么对象good@kolossus:不需要患者的设置器。JSF/EL在患者
自身中调用setter。例如,managedBean.getPatient().setFirstName(firstName)
。问题确实出在getter中。每次它都会覆盖初始值,因此所有提交的值都会丢失。我发现这里有很多问题需要解决:
是不必要的。#{managedBean.patient}
的存在意味着您可以像无JSTL一样访问它。2.除非您只是决定不给我们看,否则没有患者的setter
。在没有setter的模型更新期间,不会将任何内容保存到支持bean。您在getPatient()
中的整个方法都是错误和混乱的。您有
一个pageScoped
carpatient
并且在您的后台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";
}
}