Bug in <p:selectOneMenu>?

UI Components for JSF
Post Reply
msh321
Posts: 26
Joined: 30 May 2011, 15:01

16 Nov 2011, 00:11

Hi all,

I have spent quite some time on something that (to me) looks like a bug in PrimeFaces. I have now narrowed it down to a simple example -- I hope that it can be of use in fixing the bug. :)

Here is my example:

DropDownBean.java (DDB):

Code: Select all

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

@ManagedBean(name = "DDB")
@ViewScoped
public class DropDownBean implements Serializable
{
  private static final long serialVersionUID = 7735251946389945740L;

  public List<SelectItem> getValues()
  {
    List<SelectItem> res = new ArrayList<SelectItem>();
    res.add(new SelectItem("1", "A"));
    res.add(new SelectItem("2", "B"));
    res.add(new SelectItem("3", "C"));
    res.add(new SelectItem("4", "D"));
    return res;
  }

  public String getInitValue()
  {
    return "2";
  }

  public void valueChanged()
  {
    System.out.println("!!!CHANGED!!!");
  }
}
And here is a JSF/XHTML file:

dd1.xhtml:

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
  <f:view>
    <h:form>
      <p:selectOneMenu value="#{DDB.initValue}" effect="fade" effectDuration="100">
        <f:selectItems value="#{DDB.values}" />
        <p:ajax listener="#{DDB.valueChanged}" event="change" />
      </p:selectOneMenu>
    </h:form>
  </f:view>
</h:body>
</html>
If I run this example, I can see that the page is rendered as expected -- i.e. I see a dropdown and the item "2" is selected by default. However, if I select another item from the dropdown, then no event is triggered. I can see this because the string "!!!CHANGED!!!" is NOT written to STDOUT.

I then copied "dd1.html" to "dd2.html" and removed the "value" attribute the from <p:selectOneItem> element. Here's the source code for dd2.xhtml:

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
  <f:view>
    <h:form>
      <p:selectOneMenu effect="fade" effectDuration="100">
        <f:selectItems value="#{DDB.values}" />
        <p:ajax listener="#{DDB.valueChanged}" event="change" />
      </p:selectOneMenu>
    </h:form>
  </f:view>
</h:body>
</html>
Now, when I try out "dd2.xhtml" if can see that the event is triggered if I select a value from the dropdown (i.e. "!!!CHANGED!!!" is written to STDOUT).

In my application, I need to be able to set the initial value of <p:selectOneMenu> and at the same time I need to trigger an AJAX request whenever the value is changed. Unfortunately, I cannot get the two things to work at the same time. To me, this looks like a bug in PrimeFaces. Has anyone else seen this problem? Is there a workaround?

Best regards,
Mads

BTW: I have used the PrimeFaces version from SVN (I built it yesterday) with Mojarra 2.1.3. I have also tried with a version that I built from SVN a month ago, and here I get the same problem.

bpap
Posts: 34
Joined: 30 Sep 2011, 16:15

16 Nov 2011, 00:46

Well, I don't know if the change event should be triggerd or not when the getter always return "2", but that's not the way to go. You should have a field in your bean to keep the selected value (pre-initialized to the value you want) and a respective getter and setter.

Code: Select all

private String value = "2";

public getValue() {
  return value;
}

public setValue(String value) {
  this.value = value;
}
The listener is not needed.

Code: Select all

<p:selectOneMenu value="#{DDB.value}" effect="fade" effectDuration="100">
        <f:selectItems value="#{DDB.values}" />
</p:selectOneMenu>
Hope this helps.
Apache Tomcat 7.0.21 / JSF 2.1.2 / PM 3.0.M4-SNAPSHOT

msh321
Posts: 26
Joined: 30 May 2011, 15:01

16 Nov 2011, 01:09

Hi,

Thanks for the reply.

I have tried to replace my "hard-coded" method with a getter and setter as you suggested, but I still have the same problem as before.

BTW: I don't understand why I don't need a listener. I wish to trigger an AJAX update immediately when a new selectOneMenu value has been selected, so I need the <p:ajax> element -- or am I wrong?

Best regards,
Mads

bpap
Posts: 34
Joined: 30 Sep 2011, 16:15

16 Nov 2011, 01:35

Is that working ok if you use h:selectOneMenu/f:ajax instead?
I meant you don't need a listener to store the selected value in the backing bean.

EDIT: The following code works just fine here

DropDownBean.java

Code: Select all

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

@ManagedBean(name = "DDB")
@ViewScoped
public class DropDownBean implements Serializable {
	private static final long serialVersionUID = 7735251946389945740L;

	public List<SelectItem> getValues() {
		List<SelectItem> res = new ArrayList<SelectItem>();
		res.add(new SelectItem("1", "A"));
		res.add(new SelectItem("2", "B"));
		res.add(new SelectItem("3", "C"));
		res.add(new SelectItem("4", "D"));
		return res;
	}

	public String value = "2";

	public String getValue() {
		return value;
	}
	
	public void setValue(String value) {
		this.value = value;
	}

	public void valueChanged() {
		System.out.println("!!!CHANGED!!!");
	}
}
dd1.xthml

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
	<f:view>
		<h:form>
			<p:selectOneMenu value="#{DDB.value}" effect="fade" effectDuration="100">
				<f:selectItems value="#{DDB.values}" />
				<p:ajax listener="#{DDB.valueChanged}" event="change" />
			</p:selectOneMenu>
		</h:form>
	</f:view>
</h:body>
</html>
Apache Tomcat 7.0.21 / JSF 2.1.2 / PM 3.0.M4-SNAPSHOT

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 53 guests