0

I need to return the selected value to the bean for further processing but its returning null value .

<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>

and the backing beaning is

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

I have edited the code snippet i guess it will give you a insight on it .

Rajeev Massey
  • 137
  • 1
  • 6
  • 20
  • Are your components inside a form? – Emil Kaminski Mar 13 '14 at 06:59
  • i did . but still not working – Rajeev Massey Mar 13 '14 at 07:03
  • The method `getValues()` is well invoked ? – Omar Mar 13 '14 at 08:37
  • First change `f:ajax` by `p:ajax`, second `MenuBean.stateList` is a list of **Entity** or **String**, if are first you need a **converter**. Please write code method `stateChange` – Mathew Rock Mar 13 '14 at 08:54
  • @Omar the method is well invoked – Rajeev Massey Mar 13 '14 at 09:04
  • @MathewRock with p:ajax its not working second the stateList is an arraylist of strings .. – Rajeev Massey Mar 13 '14 at 09:05
  • It is difficult to understand what you really have on the page. Can you add more xhtml code here, so that we would see the form. The only thing that I can say for sure that when listener #{MenuBean.stateChange} is called on change of select menu it will not have new value that is assigned to the #{MenuBean.state} because you have immediate="true" attribute there. Immediate skips any model updates. It just calls the method. – trims Mar 13 '14 at 09:11
  • The bean is not so interesting as the page xhtml. I want to see the everything that is inside the – trims Mar 13 '14 at 09:44
  • @trims i have update the post with all the required information – Rajeev Massey Mar 13 '14 at 09:52

2 Answers2

1

Change your method stateChange to :

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

And change <f:selectItems value="#{MenuBean.stateList}"></f:selectItems> to <f:selectItems value="#{MenuBean.stateList}" var="state" itemValue="state.id" itemLabel="state.id"/>

Mathew Rock
  • 989
  • 2
  • 14
  • 32
0

So after you posted full xhtml I see the reason why it is not working.

you have on the page <p:commandButton value="Submit" immediate="true". You have immediate attribute which means no values from inputs or dropdowns will be set to you beans. Immediate attribute forces jsf to skip all phases except invoke application and render response. To read more about jsf phases read this post

You need to remove this attribute from submit button and it should start working. Also according to this post in primefaces update="@form". So your button will look like this:

<p:commandButton value="Submit" update="@form" action="#{MenuBean.getValues}"
Community
  • 1
  • 1
trims
  • 453
  • 4
  • 9