2014-03-13 33 views
0

我需要將選定的值返回給bean進行進一步處理,但它的返回空值。選擇一個菜單返回Null值在Backing bean中?

<h:form> 

<h:panelGrid columns="1"> 
        <p:outputLabel id="state" value="State Name"></p:outputLabel> 
        <p:selectOneMenu id="statemenu" style="width:300px;" 
         value="#{MenuBean.state}"> 
         <f:selectItem itemLabel="Select One"></f:selectItem> 
         <f:selectItems value="#{MenuBean.stateList}"></f:selectItems> 

         <p:ajax listener="#{MenuBean.stateChange}" update="dist" 
          process="@this" immediate="true"></p:ajax> 

        </p:selectOneMenu> 

        <br></br> 
        <p:outputLabel value="District"></p:outputLabel> 
        <p:selectOneMenu id="dist" style="width:300px;" 
         value="#{MenuBean.district}"> 
         <f:selectItem itemLabel="Select One"></f:selectItem> 
         <f:selectItems value="#{MenuBean.districtList}"></f:selectItems> 
        </p:selectOneMenu> 
       </h:panelGrid> 

<h:panelGrid style="position:relative; left:165px" columns="2" 
       cellpadding="2" cellspacing="2"> 
       <p:commandButton value="Reset" 
        style='font-family: font-family : Baskerville, "Baskerville Old Face", 
    "Hoefler Text", Garamond, "Times New Roman", serif;; 
font-size: 13px; font-weight: normal'></p:commandButton> 
       <p:commandButton value="Submit" immediate="true" 
        action="#{MenuBean.getValues}" 
        style='font-family: font-family : Baskerville, "Baskerville Old Face", 
    "Hoefler Text", Garamond, "Times New Roman", serif;; 
font-size: 14px; font-weight: normal'></p:commandButton> 
      </h:panelGrid> 
</h:form> 

和背襯beaning是

私人字符串狀態;

private String district; 
     private Map<String,String> StateList = new HashMap<String,String>();; 
     private Map<String,String> DistrictList = new HashMap<String,String>(); 




public MenuBean() { 
     System.out.println("Entering the Constructor"); 
     StateList = DBConnector.StateList(); 
     // DistrictList = DBConnector.DistrictList(); 
    } 



public void stateChange() { 
      DistrictList = DBConnector.DistrictList(); 
    } 
public void getValues() { 
     System.out.println("getting the values"); 
     System.out.println(getState()); 
     System.out.println(getDistrict()); 
    } 

我編輯了代碼段,我猜它會給你一個洞察力。

+0

您的組件是否在表單中? –

+0

我做到了。但仍然不起作用 –

+0

getValues()方法調用的很好嗎? – Omar

回答

0

更改方法stateChange到:

public void stateChange(AjaxBehaviorEvent event) { 
    setState(event.getComponent().getAttributes().get("value")); 
    DistrictList = DBConnector.DistrictList(); 
} 

,並更改<f:selectItems value="#{MenuBean.stateList}"></f:selectItems><f:selectItems value="#{MenuBean.stateList}" var="state" itemValue="state.id" itemLabel="state.id"/>

+0

仍然不能正常工作 –

+0

您是否嘗試更改'f:selectItems' @ user3383174 –

+0

是的。但仍然無法正常工作 –

0

你貼全XHTML所以之後我明白爲什麼它不工作的原因。

您在頁面上有<p:commandButton value="Submit" immediate="true"。你有immediate屬性,這意味着沒有輸入值或下拉菜單的值將被設置給你的bean。直接屬性強制jsf跳過除調用應用程序和呈現響應之外的所有階段。閱讀更多關於jsf階段閱讀this post

您需要從提交按鈕中刪除此屬性,它應該開始工作。同樣根據update="@form"中的post。因此,您的按鈕將如下所示:

<p:commandButton value="Submit" update="@form" action="#{MenuBean.getValues}" 
+0

仍然沒有工作..所以有沒有其他方式。 –

+0

嘗試添加進程=「@ form」 – trims

+0

獲取值 null null 此問題仍然存在 –

相關問題