我有一些 JSF 页面和一些 bean,因为它正在编辑当前元素,我使用更新,否则我使用保存方法。
我试图调试并认为按钮上的方法永远不会执行
这是我的 JSF 页面:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Manage panel</title>
</h:head>
<h:body>
<h3>Add/edit patient</h3>
<h:form>
<c:set var="patient" value="#{manageBean.patient}" />
<c:choose>
<c:when test="#{patient!=null}">
<p:panel id="panel1" header="Patient" style="margin-bottom:10px;">
<h:panelGrid columns="2">
<h:outputLabel for="name" value="First name" />
<p:inputText id="firstName" required="true" value="#{patient.firstName}" />
<h:outputLabel for="name" value="Family name" />
<p:inputText id="familyName" required="true" value="#{patient.familyName}" />
<h:outputLabel for="name" value="Sex" />
<p:selectOneMenu id="sex" value="#{patient.sex}">
<f:selectItems value="#{manageBean.sex}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Birthday date" />
<p:calendar value="#{patient.birthdayDate}" mode="inline" id="birthdayDate"/>
<h:outputLabel for="name" value="Nationality" />
<p:selectOneMenu id="nationality" value="#{patient.nationality}">
<f:selectItems value="#{manageBean.nationality}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Adress" />
<p:inputText id="adress" required="true" value="#{patient.adress}" />
<h:outputLabel for="name" value="Phone number" />
<p:inputMask id="phoneNumber" required="true" value="#{patient.phoneNumber}" mask="(999) 999-9999"/>
</h:panelGrid>
</p:panel>
<p:commandButton value="Update" type="submit" action="#{manageBean.update(patient)}" />
</c:when>
<c:otherwise>
<p:panel id="panel2" header="Patient" style="margin-bottom:10px;">
<h:panelGrid columns="2">
<h:outputLabel for="name" value="First name" />
<p:inputText id="firstName" required="true" value="" />
<h:outputLabel for="name" value="Family name" />
<p:inputText id="familyName" required="true" value="" />
<h:outputLabel for="name" value="Sex" />
<p:selectOneMenu id="sex" value="">
<f:selectItems value="#{manageBean.sex}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Birthday date" />
<p:calendar mode="inline" id="birthdayDate"/>
<h:outputLabel for="name" value="Nationality" />
<p:selectOneMenu id="nationality" value="">
<f:selectItems value="#{manageBean.nationality}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Adress" />
<p:inputText id="adress" required="true" value="" />
<h:outputLabel for="name" value="Phone number" />
<p:inputMask id="phoneNumber" required="true" value="" mask="(999) 999-9999"/>
</h:panelGrid>
</p:panel>
<p:commandButton value="Save" action="#{manageBean.save(firstName, familyName, sex, birthdayDate, nationality, adress, phoneNumber)}" />
</c:otherwise>
</c:choose>
</h:form>
</h:body>
</html>
这是我的bean方法:
public String update(Patient patient) {
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(patient);
tx.commit();
} catch (HibernateException ex) {
if (tx != null) {
tx.rollback();
}
ex.printStackTrace();
} finally {
session.close();
}
return "go_home";
}
public String save(String fitstName, String familyName, Sex sex, Date birthdayDate, Nationality nationallity, String adress, String phoneNumber){
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Patient patient = new Patient();
patient.setFirstName(fitstName);
patient.setFamilyName(familyName);
patient.setSex(sex);
patient.setBirthdayDate(birthdayDate);
patient.setNationality(nationallity);
patient.setAdress(adress);
patient.setPhoneNumber(phoneNumber);
session.save(patient);
tx.commit();
} catch (HibernateException ex) {
if (tx != null) {
tx.rollback();
}
ex.printStackTrace();
} finally {
session.close();
}
return "go_home";
}