2011-08-07 23 views
0

我需要輸出在selectOneMenu -list中選擇的文本。我的代碼如下;選擇一個組件並顯示 - Ajax

<h:selectOneMenu value="#{DataForm.stationed}" id="globalFilter" onchange="carsTable.filter()" style="width:350px;font-size:13px;" > 
    <f:selectItems value="#{DataForm.listHotel}" var="user" itemValue="#{user[1]}" itemDisabled="false" itemLabel="#{user[1]}" /> 
    <h:outputText value="#{carsTable[1]}" style="width:350px"/> 
</h:selectOneMenu> 

我該如何編碼?

編輯

這個列表框是內<datatable>

<h:body> 
    <h:form id="form1" > 
     <p:dataTable var="car" value="#{DataForm.listHotels}" widgetVar="carsTable" paginator="true" rows="10" onRowSelectComplete="carDialog.show()" emptyMessage="No hospital found with given criteria" selectionMode="single" onRowSelectUpdate="growl" style="width:1400px;font-size:13px;"> 

      <h:selectOneMenu value="#{DataForm.stationed}" id="globalFilter" onchange="carsTable.filter()" style="width:350px;font-size:13px;" > 
       <f:selectItems value="#{DataForm.listHotel}" var="user" itemValue="#{user[1]}" itemDisabled="false" itemLabel="#{user[1]}" /> 
       <h:outputText value="#{carsTable[1]}" style="width:350px"/> 
      </h:selectOneMenu> 

     </p:dataTable> 

     <f:ajax render= "@form1" > 
      <h:selectOneMenu value="#{DataForm.stationed}"> 
       <f:selectItems value="#{DataForm.listHotels}" var="item" itemValue="#{DataForm.listHotels}" itemLabel="#{DataForm.listHotels}" /> 
      </h:selectOneMenu> 
     </f:ajax> 
    </h:form> 
</h:body> 

回答

0

我想你應該刪除嵌套<h:outputText/>第一,它並沒有真正意義在那個位置。此外,從您的問題和示例代碼中,PrimeFaces不得不處理它。

要輸出所選值,只需打印表達式#{DataForm.stationed},因爲這是將接收選定值的綁定。

以下示例通過AJAX演示了這一點,但如果您使用動作和常規表單提交,它將以相同的方式工作。

的facelet

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 
    <h:head /> 

    <h:body> 

     <h:form id="form"> 

      <f:ajax render="form"> 
       <h:selectOneMenu value="#{selectOneMenuBean.value}"> 
        <f:selectItems value="#{selectOneMenuBean.items}" var="item" itemValue="#{item}" itemLabel="#{item}" /> 
       </h:selectOneMenu> 
      </f:ajax> 

      Selected value: #{selectOneMenuBean.value} 

     </h:form> 

    </h:body> 
</html> 

@ViewScoped 
@ManagedBean 
public class SelectOneMenuBean { 

    private List<String> items = Arrays.asList("a", "b", "c"); 
    private String value; 

    public List<String> getItems() { 
     return items; 
    } 

    public String getValue() { 
     return value; 
    } 

    public void setValue(String value) { 
     this.value = value; 
    } 

} 

附: 在您的代碼示例中,您的bean的EL名稱以大寫字母開頭。這與典型的慣例有點相反。另外,你可能想要注意你的名字和類型。這個bean返回一個叫做listHotel的東西,但是它被分配給一個名爲user的變量,然後它被一個整數索引。我會建議對齊名稱(例如,集合名稱= users,變量名稱user)並使用屬性而不是索引(例如user.name)。

+0

你好,我得到這個異常,任何線索? 'SEVERE:錯誤渲染視圖[/test.xhtml] javax.faces.FacesException:包含一個未知的id'form' - 無法找到它在組件j_idt15'的上下文中 – Illep

+0

您需要給你的表單id 'form'以便通過ID引用它。在這個例子中,它是一個簡單的ID,這意味着它必須可以從ajax標籤中獲得。嘗試給出的例子,並仔細修改它的情況,直到它中斷。或者,您可以在ajax標籤上使用'render =「@ form」'。這將找到你的組件嵌套的形式,而不用擔心名稱。 –

+0

我編輯了我的問題,增加了更多信息。即使我添加了'render =「@ form」'我仍然得到同樣的異常'' – Illep

相關問題