0
votes

I am facing a weird issue with h:selectOneMenu component.

I am using an h:selectOneMenu in my JSF 2 application. I created an Arraylist of months in my controller class which is given to the h:selectOneMenu. The starting element of the dropdown is “Any” whose value is a blank string. The value of the h:selectOneMenu is tied to an object class variable “month”. Variable month is initialized as “” (empty string).

I have created a ValueChangeEvent on h:selectOneMenu. I have written a method changeMonth(event) in controller which is invoked on Value change event.

My issue is that this value change event is triggered when I click the submit button even though the value is not getting changed. This happens only when the dropdown is at “Any”. When I tried to get the values for the change in the changeMonth method I get the old value as “”(Blank string) and the new value as null.

Ideally the ValueChangeEvent should not be triggered. But since its changing from blank to null this is happening. Why does this happen? Is it an issue with the JSF 2 h: selectOneMenu component?

I am pasting my Controller code:

@ManagedBean
@SessionScoped
public class Controller {

private List monthList;
private ObjClass searchVO;

public List getMonthList() {
    List<SelectItem> list = new ArrayList<SelectItem>();
    list.add(new SelectItem("", "Any"));

    for (int i = 1; i <= 12; i++) {
        list.add(new SelectItem(String.valueOf(i), "Month"+i));
    }
    monthList = list;
    return monthList;
}

/**
 * @param monthList
 *            the monthList to set
 */
public void setMonthList(List monthList) {

    this.monthList = monthList;
}
public ObjClass getSearchVO() {
    if (searchVO == null) {
        searchVO = new ObjClass();
    }
    return searchVO;
}

public void setSearchVO(ObjClass searchVO) {
    this.searchVO = searchVO;
}

public void changeMonth(ValueChangeEvent changeEvent) {

    PhaseId phaseId = changeEvent.getPhaseId();
    String oldValue = (String) changeEvent.getOldValue();
    String newValue = (String) changeEvent.getNewValue();

}

}

This is my Object Class:

public class ObjClass {
public String getMonth() {
    return month;
}

public void setMonth(String month) {
    this.month = month;
}

private String month="";

public ObjClass(String month) {
    super();
    this.month = month;
}
public ObjClass() {
}

}

And this is my xhtml form:

<h:form>


    Dropdown example:

    <h:selectOneMenu valueChangeListener="#{controller.changeMonth}"
                onchange="submit();" id="month"
                value="#{controller.searchVO.month}" title="Select Month">
                <f:selectItems value="#{controller.monthList}" />
    </h:selectOneMenu>

        <br /><br />

  <h:outputText value="#{controller.searchVO.month}"></h:outputText>
            <h:commandButton value="Submit" action="result" />
        <h:commandButton value="Reset" type="reset" />

    </h:form>
1

1 Answers

0
votes

Instead of adding SelectItem("", "Any") to the list, try to replace it by:

list.add(new SelectItem(null, "Any", null, false, true, true));

The point is to use the noSelectionOption attribute of SelectItem (the last argument in the constructor for which we assigned true) which indicates thats its the "no selection" option, more informations about that attribute could be found in SelectItem's Javadoc:

Flag indicating whether the option created by this component represents the special "no selection" option. Expressions must evaluate to a boolean. Default value is false.

Regarding the SelectItem's contructor used above, this is the description provided by Javadocs:

SelectItem

public SelectItem(java.lang.Object value,
                  java.lang.String label,
                  java.lang.String description,
                  boolean disabled,
                  boolean escape,
                  boolean noSelectionOption)

Construct a SelectItem instance with the specified property values.

Parameters:

value - Value to be delivered to the model if this item is selected by the user
label - Label to be rendered for this item in the response
description - Description of this item, for use in tools
disabled - Flag indicating that this option is disabled
escape - Flag indicating that the text of this option should be escaped when rendered.
noSelectionOption - Flag indicating that the current option is a "no selection" option