File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Problem using valueChangeListener and onchange Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Problem using valueChangeListener and onchange" Watch "Problem using valueChangeListener and onchange" New topic
Author

Problem using valueChangeListener and onchange

Ian Dunsirn
Ranch Hand

Joined: May 25, 2006
Posts: 36
Hello,

I am having some troubles with JSF. I have a page that has a selectOneListbox in it. What I want to have happen is that when the user picks an item in the listbox, the page will execute a method in our listener class. What is happening is that the page gets reloaded, but the listener method is not being fired off. My JSF code is:

<h:form>
<f:verbatim>
<TABLE>
<TR>
<TD>
<B><hutputText value="Field Name: "/></B>
</TD>
<TD>
<h:selectOneListbox id="uiSelectionField" size="1"
valueChangeListener="#{projectListener.uiInitialSelect}"
onchange="this.form.submit()" immediate="true">
<f:selectItem itemValue="Environment" itemLabel="Environment"/>
<f:selectItem itemValue="Status" itemLabel="Status"/>
<f:selectItem itemValue="Priority" itemLabel="Priority"/>
<f:selectItem itemValue="Release" itemLabel="Release"/>
<f:selectItem itemValue="Issue Type" itemLabel="Issue Type"/>
<f:selectItem itemValue="Function" itemLabel="Function"/>
<f:selectItem itemValue="Module" itemLabel="Module"/>
</h:selectOneListbox>
</TD>
</TR>
</TABLE>
</f:verbatim>
</h:form>

What are we doing wrong?

Thanks.
Ian Dunsirn
Ranch Hand

Joined: May 25, 2006
Posts: 36
Sorry about the repost, I got some kind of error trying to post and thought the post hadn't gone through.
[ September 13, 2007: Message edited by: Ian Dunsirn ]
Krithika Srinath
Ranch Hand

Joined: Apr 11, 2006
Posts: 52
I dont see anything obviously wrong with the given code. Can you elaborate on how do you determine that the valueChangeListener is not notified or the method in your backing bean in not getting called..?
Are you getting any error message or you cant see the expected behaviour in your page while you submit - please explain or if required provide backing bean code as well.
Ian Dunsirn
Ranch Hand

Joined: May 25, 2006
Posts: 36
I put in some displays in the method I am invoking and I do not see it when I look at my console in Eclipse. I see displays that are in other methods, but not that one. I am not at work and can't put up that code at the moment.
Hedzar Terabos
Greenhorn

Joined: Sep 14, 2007
Posts: 3
Ian
if possible post the error message..so that i can go thru and provide you the solution

Hedzar
Ian Dunsirn
Ranch Hand

Joined: May 25, 2006
Posts: 36
And there is the rub. We are not getting any error messages at all. I even misspelled the name of the invoked method on purpose to get an error. Nothing. The page does reload, but none of the displays we put in are showing up. We are newbs and at a loss as to what we may be missing.
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
I don't see the "value" attribute in your h:select...Could it be it?
Ian Dunsirn
Ranch Hand

Joined: May 25, 2006
Posts: 36
I was under the impression that value would be needed only if we were populating the listbox with data from a table. If I am incorrect, let me know.
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
It is the f:verbatim tag that's causing the problem.
I tested your code after removing that tag and it worked fine for me. Try not to include your component under that. f:verbatim's purpose is different...see the documentation.

Regarding the page reload its only because you are using javascript onchange and submit and is not part of your JSF actions.

And you were right that value attribute is not needed just to call the valueChangeListener. However if you want to capture the selected value, you do need it.
[ September 14, 2007: Message edited by: A. Dusi ]
Ian Dunsirn
Ranch Hand

Joined: May 25, 2006
Posts: 36
So, do I need the onchange=submit then?
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
If you are only looking to capture your listbox selection, you dont need the valueChangeListener.
Simply provide the value attribute mapping to a backing bean's property of String type(your itemValues are Strings). On form submission(by a click of a button or onchange=submit), the selection will automatically be posted to your backing bean's property.

Note that the form submission using javascript onchange=submit is useful only if there are other components on your page that need to be populated dynamically based on your listbox selection. In all other cases, a simple button submit will do.
By using javascript onchange=submit, you make the selected value post to your backing bean immediately.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Funny, I just did this last week. OK, so if you want a valueChangeListener to say change the values in a DataTable or in another combobox, there are a couple of things that you will need.

1. There are two ways to declare your valueChangeListener. One is the a <valueChangeListener> tag which requires a fully qualified class name, and that Class must implement the ValueChangeListener interface. The other, which is what I am going to talk about is the valueChangeListener attribute for any UIInput tag, which has the EL link to a method in a backing bean.

OK, so for the attribute.

set the EL to the method. Also set an onChange attribute to == "submit()" you most likely will want immediate="true" also in that tag.

In your method code, at the end of the code you will want to do the method call to go directly to the renderPhase, so something like FacesContext.getInstance.renderPhase() I forgot the last method name, but it should be easy enough to find.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Raul Pinheiro
Greenhorn

Joined: Sep 15, 2009
Posts: 2
Hi everyone! im new around here, and as i was looking for a solution to the problem im facing during my jsf development, i've found this post. Almost explained what should i do to solve it. Im facing the same problem, my valueChangeListener is not called any time! i wrote some sysout line codes to catch it on eclipse console, but nothing happens .

Im trying to find out what is the difference


the structure is showed below:

##index.jsp##


## newBridgeInputForm.jsp ##


## managedBean ##



As you can see, the XinputForm.jsp is loaded based on the item chosen in the first <h:selectOneMenu>. In this case im not using any <f:verbatim> tag, and it just refuse to work properly

Help me, please!

Thanks anyway!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem using valueChangeListener and onchange