0

I'm doing a project on jsf and i'm using a select one menu that allows the user to choose an item and them press submit. When the button is clicked the jsf call the bean and it should receive the value selected but i always get a empty result. I saw other post and i guess i'm pretty close of others answers, i guess it just a small detail that is missing

Here is my code:

        <h:selectOneMenu id="sel1" value="#{foo.bankName}">
            <f:selectItems value="#{foo.bankNameMap}" />
        </h:selectOneMenu>

<h:commandButton id="validate" value="Submit"
                            action="#{foo.submit}"/>

And now on bean:

 public String getBankName() {
            return bankName;
        }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    private static Map<String,Object> bankNameMap;

    public Map<String,Object> getBankNameMap() {
        return bankNameMap;
    }

    @PostConstruct
    public void init() {
            initiateDropDown();
     } 

     public void  initiateDropDown() {
            bankNameMap = new LinkedHashMap<String,Object>();
             bankNameMap.put("aaaa" , "aaaa");
     }

    public void submit(){ //method called when clicked on the button
          System.out.println("item " +bankName);
    }

output:

item

Thanks in advance

Tazz
  • 79
  • 13
  • Possible duplicate of [commandButton/commandLink/ajax action/listener method not invoked or input value not updated](https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value) – Kukeltje Jul 07 '17 at 08:48

1 Answers1

0

You created a method initiateDropDown(); to populate map, but you never called it. I suppose that you can not select aaaa from <h:selectOneMenu/> component, because it does not exist in map. Your map is empty. Also, return type is missing on your submit() method. You need to set return type of method to pass compile time. For example public void submit();.

The simplest way to get existing code work should be:

public Map<String, Object> getBankNameMap() {
    initiateDropDown();
    return bankNameMap;
}

public void submit() { // method called when clicked on the button
    System.out.println("item " + bankName);
}

JSF seems pretty good . Just make sure your elements are in <h:form>.

<h:body>
    <h:form id="myForm">
        <h:selectOneMenu id="sel1" value="#{foo.bankName}">
            <f:selectItems value="#{foo.bankNameMap}" />
        </h:selectOneMenu>
        <h:commandButton id="validate" value="Submit" action="#{foo.submit}"/>
    </h:form>
<h:body>
stakahop
  • 921
  • 9
  • 18
  • Sorry i forget to put the constructor code .. the construtor is called when the jsf is initialized and the constructor call the initiateDropDown and fill the dropdown.. I thought I had conveyed the idea that the dropdown worked the problem is pass the selected item to the bean I will edit the post – Tazz Jul 06 '17 at 16:40
  • No this is my structure to the select one menu:
    < div> selectOneMenu
    .. do i need a h:form?
    – Tazz Jul 07 '17 at 07:58
  • i put a h:form around the select menu but nothing special happens – Tazz Jul 07 '17 at 08:03
  • its true, the form was the problem.. i just added it and now its working thank you! – Tazz Jul 07 '17 at 08:22
  • You are welcome :) . You should always use it in your JSF pages. The is the component that’s responsible for allowing the to submit the view back to the server. – stakahop Jul 07 '17 at 08:28
  • @stakahop: For many of these 'basic' problems, there are already good Q/A in Stackoverflow. Please search for them and mark the new question as a duplicate! Keeps stackoverflow clean AND informs new users of good Q/A with lots of additional info – Kukeltje Jul 07 '17 at 08:49