在Backing bean中选择返回Null值的One Menu



我需要将所选值返回给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>

,背轴承

private String state;

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

我已经编辑了代码片段,我想它会给你一个洞察。

将方法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"/>

所以在你发布了完整的xhtml之后,我看到了它不工作的原因。

显示在<p:commandButton value="Submit" immediate="true"页上。您有immediate属性,这意味着不会将输入或下拉框中的值设置为bean。Immediate属性强制jsf跳过除了调用应用程序和呈现响应之外的所有阶段。要了解更多关于jsf阶段的信息,请阅读这篇文章

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

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

相关内容

  • 没有找到相关文章

最新更新