0

我有一些 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";
    }
4

1 回答 1

1

一旦你在你的输入标签中给出了 value 属性,它就会直接将给定的值绑定到 java 类。在您的 java 类中创建类变量并绑定输入标签。EX:- 在您的 bean 类中创建变量,例如

private String firstName 

并创建 getter 和 setter 方法

<f:view>

    <h3>Add/edit patient</h3>

    <h:form>           
                <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="#{manageBean.firstName}" />

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

                        <h:outputLabel for="name" value="Sex" />  
                        <p:selectOneMenu id="sex" value="#{manageBean.sex}">  
                            <f:selectItems value="#{manageBean.sex}"/>
                        </p:selectOneMenu>  

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

                        <h:outputLabel for="name" value="Nationality" />  
                        <p:selectOneMenu id="nationality" value="#{manageBean.nationality}">  
                            <f:selectItems value="#{manageBean.nationality}"/>
                        </p:selectOneMenu>  

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

                        <h:outputLabel for="name" value="Phone number" />  
                        <p:inputMask id="phoneNumber" required="true" value="#{manageBean.phoneNumber}" mask="(999) 999-9999"/>
                    </h:panelGrid>  
                </p:panel>  
                <p:commandButton value="Update" type="submit" action="#{manageBean.update}" />  

                    </h:form> 

创建一个新的 jsp 页面并将这些代码复制粘贴到 body 标记中。在您的 java 类中创建变量和 getter setter 方法。更新应该是一个返回字符串的方法。

于 2013-01-26T17:56:42.300 回答