我有一些 JSF 页面和一些管理 bean。在管理 bean 中,我创建了一些 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>
还有我的 ManageBean:
@ManagedBean(name = "manageBean")
@SessionScoped
public class ManageBean implements Serializable {
private Patient patient;
private SessionFactory factory;
public ManageBean() {
factory = SessionFactoryWrap.getInstance();
}
public Patient getPatient() {
patient = (Patient) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("patient");
if (patient == null) {
//patient = new Patient("", "", Sex.male, new Date(), Nationality.ukranian, "", "");
patient = new Patient();
}
return 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();
} finally {
session.close();
}
patient=null;
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("patient", null);
return "go_home";
}
}