选择一个菜单在Backing bean中返回Null值?

时间:2014-03-13 05:35:41

标签: jsf primefaces

我需要将选定的值返回给bean进行进一步处理,但是返回null值。

<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());
    }

我已经编辑了代码片段,我想它会让你对它有所了解。

2 个答案:

答案 0 :(得分: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"/>

答案 1 :(得分:0)

因此,在您发布完整的xhtml之后,我会看到它无法正常工作的原因。

您在页面<p:commandButton value="Submit" immediate="true"上。您有immediate属性,这意味着您的bean不会设置输入或下拉列表中的值。立即属性强制jsf跳过除调用应用程序和呈现响应之外的所有阶段。要阅读有关jsf阶段的更多信息,请阅读this post

您需要从提交按钮中删除此属性,它应该开始工作。同样根据这个post在primefaces update="@form"中。所以你的按钮看起来像这样:

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