Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF h:selectOneMenu is never set even when using f:ajax
    text
    copied!<p>I want to implement a filtering facility in a JSF web application as follows: The users can add as many filters as they want. They can also delete them. So I am having a dataTable of filters. Each row consists of one h:selectOneMenu which has an ajax “change” event in order to make a second h:selectOneMenu visible in the same row. The options of the second h:selectOneMenu are calculated dynamically according to the selected option of the first. </p> <p>The problem is that the value of second h:selectOneMenu is never set to the back-end object even if I added an ajax event. However the value of the first h:selectOneMenu is set.</p> <p>I have the following fragment of code in an .xhtml page:</p> <pre><code>&lt;h:form id="filterForm"&gt; &lt;h:dataTable id="filterTable" value="#{filterManager.filters}" var="filter"&gt; &lt;h:column&gt; &lt;h:outputLabel value="#{msgs.filterBy}:" for="availableFilters" /&gt; &lt;h:selectOneMenu id="availableFilters" value="#{filter.filter}"&gt; &lt;f:selectItems value="#{filterManager.getProperties(typeSelector.typeSelected)}" /&gt; &lt;f:ajax event="change" render=":filterForm" /&gt; &lt;/h:selectOneMenu&gt; &lt;/h:column&gt; &lt;h:column&gt; &lt;h:panelGroup id="filterValuesPanel" &gt; &lt;h:outputLabel value="#{msgs.value}:" for="filterValues" rendered="#{!filter.filterEmpty}" /&gt; &lt;h:selectOneMenu value="#{filter.value}" id="filterValues" rendered="#{!filter.filterEmpty}" &gt; &lt;f:selectItems value="#{filterManager.getPossibleAnswers(filter)}" /&gt; &lt;f:ajax event="change" render=":filterForm" /&gt; &lt;/h:selectOneMenu&gt; &lt;/h:panelGroup&gt; &lt;/h:column&gt; &lt;h:column&gt; &lt;h:commandButton value="#{msgs.delete}" title="#{msgs.deleteFilter}"&gt; &lt;f:ajax event="click" listener="#{filterManager.removeFilter(filter)}" render=":filterForm" /&gt; &lt;/h:commandButton&gt; &lt;/h:column&gt; &lt;/h:dataTable&gt; &lt;h:commandButton value="#{msgs.addNewFilter}"&gt; &lt;f:ajax event="click" listener="#{filterManager.addNewFilter}" render=":filterForm" /&gt; &lt;/h:commandButton&gt; &lt;/h:form&gt; </code></pre> <p>I have a bean called “FilterManager” which has a ViewScoped. Important parts are shown below:</p> <pre><code>@ManagedBean @ViewScoped public class FilterManager implements Serializable { private List&lt;Filter&gt; filters; // it has a getter private int currentFilterId; public void addNewFilter(AjaxBehaviorEvent event) { this.currentFilterId++; this.filters.add(Filter.getEmptyFilter(this.currentFilterId)); } public void removeFilter(Filter filter) { this.filters.remove(filter); } ... } </code></pre> <p>The Filter class is a normal class (not a bean) and is shown below:</p> <pre><code>public class Filter implements Serializable { private int id; private String filter; private String value; public String getFilter() { return filter; } public void setFilter(String theFilter) { if (theFilter != null &amp;&amp; !theFilter.isEmpty()) this.filter = theFilter; } public String getValue() { return value; } public void setValue(String theValue) { this.value = theValue; } public boolean isFilterEmpty() { return this.filter == null || this.filter.isEmpty(); } ... } </code></pre> <p>Notice that TypeSelector is a SessionScoped bean which has a typeSelected property along with getter and setter.</p> <p>The problem is: filter.filter is set correctly whereas filter.value is never set. I can't find the problem so I need your help please. Apologies for all this code but I needed to provide you with all the necessary details.</p> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload