0

I am guessing this may be a noob question but I am having a very hard time finding an answer.

I have a menu which is populated from an array

<f:selectItems value="#{quizBean.categoryArray}"/>

I need to store the selected menu item in a value in QuizBean that is called tempCategory. I have a getter and setter for tempCategory called setTempCategory(String newValue) and getTempCategory().

I need to fill in the value for the selectOneListbox. quizBean.tempCategory refers to the set method for tempCategory. What would I replace newValue with so that the menu item is sent to the setTempCategory(String newValue) method?

<h:selectOneListbox value="#{quizBean.tempCategory(newValue)}">
<f:selectItems value="#{quizBean.categoryArray}"/>
</h:selectOneListbox> <br/>
<h:commandButton value="Choose Quiz" action="#{quizBean.chooseCategory(quizBean.getTempCategory)}"/>
  • All these are in Strings. I need to get the string value of the menu item selected and store it somewhere where i can retrieve it with the chooseCategory() method. – bilestrojanman Feb 07 '12 at 05:12

2 Answers2

1

Your close, here is what you should do, notice I added an <f:ajax> incase you want the value to be submitted to the bean every time you change the value of the drop down menu.

<h:selectOneListbox value="#{quizBean.tempCategory}">
    <f:selectItems value="#{quizBean.categoryArray}" var="category" itemValue="#{category.id} itemLabel="#{category.displayName}" />
    <f:ajax render="id_of_what_you_want_to_be_rerendered" />
</h:selectOneListbox>

Notice that I used itemValue and itemLabel, you can read here what they are for but basically, the ID will be passed to the value of the selectOneListBox and the label will be displayed in the UI.

The bean code should have this code:

@ManagedBean
@SessionScope  // (for example...)
public class QuizBean {
    private String tempCategory; // will store Id of current category

    public String getTempCategory() {
        return tempCategory;
    }

    public setTempCategory(String pTempCategory) {
        tempCategory = pTempCategory;
    }

    List<Category> CategoryArray;
    // .. Getter and Setter for CategoryArray
}

If you use <f:ajax> then the submit button is redundant. If you want to use the submit button just add it back and remove the <f:ajax>.

Ben
  • 10,020
  • 21
  • 94
  • 157
0

Actually JSF will automatically call getters/setters of the particular property. The selected value is set to tempCategory, you do not have to pass newValue to it. It will call the getter in the EL expression for action.

<h:selectOneListbox value="#{quizBean.tempCategory}">
  <f:selectItems value="#{quizBean.categoryArray}"/>
</h:selectOneListbox> <br/>
<h:commandButton value="Choose Quiz" action="#{quizBean.chooseCategory(quizBean.tempCategory)}"/>

But remember to pass arguments for action method you need el-api-2.2 and el-impl-2.2 jars as mentioned by BalusC for this question How to pass method arguments to an ActionListener?

Community
  • 1
  • 1
Ravi Kadaboina
  • 8,494
  • 3
  • 30
  • 42