2013-06-23 12 views
0

我有一個形式,一個簡單的JSF應用程序,其中一個控件是一個:H:selectOneListBox不工作

<h:selectOneListbox style="width: 231px; height: 27px;position:absolute;left:400px;top:325px;" value="#{PatientsSearch.selectedDoctor}"> 
    <f:selectItems value="#{PatientsSearch.doctors}" var="d" itemLabel="#{d.name}" itemValue="#{d.name}" /> 
</h:selectOneListbox> 

當我運行應用程序,我可以看到正在填充列表(即PatientsSearch。醫生正確檢索),所以我可以從中選擇一項。在我選擇了所需的項目後,沒有任何工作更多的形式。所有其他按鈕似乎都是空閒的,它們不再對點擊做出反應。但是,如果從中沒有選擇任何東西,則所有按鈕都按預期工作(即它們觸發它們在託管bean中的回調)。

我調試了應用程序,發現確實在選擇列表中的一個項目後,窗體上的其他按鈕的回調在點擊它們時不會被觸發。 同樣在調試器中,我從來沒有看到setSelectedDoctor()被調用(請參閱上面的代碼片段),如我所料。

我正在使用JSF的Mojarra 2.0.1實現。

我在這裏錯過了什麼?


更新:這是整個形式:

<h:form style="width: 876px; background-color: #FED981; padding-left: 60px; padding-top: 30px; margin-left: 80px; margin-top: 40px; color: #804000; font-style: normal; font-family: Verdana, Arial, Sans-Serif"> 
    <h:outputLabel style="position:absolute;left:200px;top:100px;" value="New appointment:"></h:outputLabel> 
    <br> 
    <br> 
    <h:inputText style="width: 259px;position:absolute;left:200px;top:120px;" binding="#{PatientsSearch.inputText1}" valueChangeListener="#{PatientsSearch.lookForName}" immediate="true" id="inputText1" /> 
    <h:commandButton value ="Search patients by name:" style="width: 173px;position:absolute;left:465px;top:120px;" action="#{PatientsSearch.getPatientsByName}"> 
     <f:param name="day" value="#{request.getParameter('day')}" /> 
     <f:param name="month" value="#{request.getParameter('month')}" /> 
     <f:param name="year" value="#{request.getParameter('year')}" /> 
    </h:commandButton> 

    <br> 
    <h:panelGroup id="pn_DETAILS_GRP" style="overflow:auto;position:absolute;top:155px;left:200px;width:300px;height:150px;solid black"> 
     <h:dataTable id="tb_USER_DETAILS" border="1" var="patient" value="#{PatientsSearch.patients}" style="width:300px;height:150px" rowClasses="oddRow, evenRow"> 
     <h:column id="name"> 
     <f:facet name="header"> 
     <h:outputText value="Name" style="font-size:10pt"/> 
     </f:facet> 
     <h:outputText value="#{patient.name}" style="font-size:8pt"/> 
     </h:column> 
     <h:column id="phone"> 
     <f:facet name="header"> 
     <h:outputText value="Phone" style="font-size:10pt"/> 
     </f:facet> 
     <h:outputText value="#{patient.phoneMobile}" style="font-size:8pt"/> 
     </h:column> 
     <h:column> 
     <h:inputHidden id="patientId" value="#{patient.id}" /> 
     </h:column> 
     </h:dataTable> 
    </h:panelGroup> 

    <h:inputHidden id="testId" /> 

    <br><br><br> 
    <h:outputLabel value="Choose doctor:" style="width: 200px;position:absolute;left:200px;top:325px;"></h:outputLabel> 
    <h:selectOneListbox style="width: 231px; height: 27px;position:absolute;left:400px;top:325px;" value="#{PatientsSearch.selectedDoctor}" size="1"> 
     <f:selectItems value="#{PatientsSearch.doctors}" var="d" 
      itemLabel="#{d.name}" itemValue="#{d.name}" /> 
    </h:selectOneListbox> 
    <br><br><br> 
    <h:commandButton value="Schedule" style="position:absolute;left:200px;top:430px;" action="#{PatientsSearch.scheduleAppointment}"> 
     <f:param name="day" value="#{request.getParameter('day')}" /> 
     <f:param name="month" value="#{request.getParameter('month')}" /> 
     <f:param name="year" value="#{request.getParameter('year')}" /> 
    </h:commandButton> 

</h:form> 
+0

您應該添加您的窗體的其他一些地區,包括按鈕。乍一看,它可能是驗證錯誤的轉換,你有一個'h:messages'的地方嗎? –

+0

這種形式沒有按鈕嗎? _所有其他按鈕似乎都是空閒的,它們不會對點擊做出反應。哪些按鈕? '#{PatientsSearch.selectedDoctor}'和'#{d.name}'的獲得者/設置者是什麼? –

+0

嗨,你可以看到整個表格 - 我已經在上面添加了它。有兩個按鈕,只有當我不在selectOneListbox中選擇任何項目時,它們才能正常工作。否則,他們的回調不會被觸發。 –

回答

0

按照意見,問題是,#{d.name}的類型是不一樣的#{PatientsSearch.selectedDoctor}。在這種情況下,有兩種解決方案:

  1. 保持您擁有相同的所有內容,將#{PatientsSearch.selectedDoctor} getter/setter更改爲String類型。
  2. 在backing bean中創建一個SelectItem的列表,並將#{PatientsSearch.selectedDoctor} getter/setter更改爲String類型。由於支持bean已經爲列表組件提供了準備好的對象,因此您還可以刪除itemValueitemLabel
  3. 變化從f:selectItemsitemValue屬性,並創建一個自定義轉換器

我喜歡第二個,因爲它更好地分離的觀點。應該改變這樣的:

<h:selectOneListbox style="width: 231px; height: 27px;position:absolute;left:400px;top:325px;" value="#{PatientsSearch.selectedDoctor}" size="1"> 
    <f:selectItems value="#{PatientsSearch.doctors}" var="d" itemLabel="#{d.name}" /> 
    <f:converter convertId="doctorConverter" /> 
</h:selectOneListbox> 

現在的轉換器,你可以這樣開始的:

@FacesConverter(value="doctorConverter") 
public class DoctorConverter implements Converter { 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 
     // Your code, select doctor from value ID for example. 
    } 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object value) { 
     // Your code, return doctor ID from value. 
    } 
} 
+0

謝謝亞歷山大,你說的話很有道理,但看起來比這更復雜。如果我使用itemValue =「#{d}」,那麼它比以前更糟:其他按鈕即使是第一次也不工作。另一種方法是使所有的字符串(即「selectedDoctor」),然後按鈕似乎工作,但選擇看起來很奇怪 - 所有醫生對象似乎序列化和連接,這就是我在我的「selectedDoctor」變量中得到的。看起來好像所有名單上的醫生都是同時選定的。這真是太瘋狂了,我相信我會放棄這個愚蠢的選擇.OneListbox –

+0

@ Sorin-AlexandruCristescu不幸的是,我把你放在一個錯誤的方向,你實際上需要一個轉換器作爲修改的答案。 –

+0

對,我希望我可以在沒有轉換器的情況下做到這一點。但也可以這樣,我會試一試,它必須工作....非常感謝。任何想法爲什麼字符串不工作(如我所解釋的)?我不應該需要一個轉換器。 –