0

Whatever I do, I can't make sense of the listener attribute of the f:ajax tag. The method won't get invoked. I don't get any error messages. Here's an example:

test.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>
            Example
        </title>
    </h:head>
    <h:body>
        <h:form>
            <h:selectOneMenu value="#{testBean.value}">
                <f:selectItem itemLabel="1" itemValue="1" />
                <f:selectItem itemLabel="2" itemValue="2" />                    
                <f:ajax render="messages" listener="#{testBean.processAjaxBehaviour}" onevent="test" />
            </h:selectOneMenu>
            <h:messages id="messages" />
        </h:form>
        <script type="text/javascript">
            function test(event)
            {
                alert(event.status);
            }
        </script>
    </h:body>
</html>

TestBean.java

package test;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.AbortProcessingException;

@ManagedBean
@ViewScoped
public class TestBean implements Serializable
{
    int value = 2;

    public int getValue()
    {
        return value;
    }

    public void setValue(int value)
    {
        value = value;
    }

    @PostConstruct
    public void init()
    {
        System.out.println("Done constructing testBean.");
    }

    public void processAjaxBehaviour(AjaxBehaviorEvent event) throws AbortProcessingException
    {
        System.out.println("Processing AJAX behaviour.");
    }
}

The function test is called three times, as expected, and I get the status "success". The listener processAjaxBehaviour isn't called however.

The init method is called. The second option element rendered by the f:selectItem gets selected as expected.

Spomf
  • 311
  • 1
  • 3
  • 7
  • Your onevent is wrong, you can use event="click" or event="change" for example. – Alexandre Lavoie Nov 27 '12 at 09:53
  • do you see any errors in the browser console ? does the alert being fired? change ` if(event.status == 'success') alert('success');` into `alert(event.status);` to see if its being stopped after the `complete` – Daniel Nov 27 '12 at 10:02
  • whats exactly not working ? `processAjaxBehaviour` not being called or js function? – Daniel Nov 27 '12 at 10:09
  • As he describe, the listener is not called. Is there a equivalent of a4j:log in pure JSF 2 that he could use to debug client side? – Alexandre Lavoie Nov 27 '12 at 10:16
  • @Daniel I updated the question. The function `test` is now called three times, as expected, and I get the status "success". The listener `processAjaxBehaviour` isn't called however. – Spomf Nov 27 '12 at 10:17
  • @Spomf any chance you declared your bean in faces-config too? – Daniel Nov 27 '12 at 10:18
  • @Daniel No, I rely on the annotation only. – Spomf Nov 27 '12 at 10:20
  • try to change `public void processAjaxBehaviour(AjaxBehaviourEvent event) throws AbortProcessingException ` into `public void processAjaxBehaviour(AjaxBehaviorEvent event) {` (remove throws) – Daniel Nov 27 '12 at 10:21
  • @AlexandreLavoie Log what client side? In case you havn't noticed; I update the messages component (but it doesn't get any messages). – Spomf Nov 27 '12 at 10:24
  • add `@PostConstruct public void init() { //some printlines here` to see if the bean is being created at all – Daniel Nov 27 '12 at 10:25
  • @Daniel It is the complete signature for an AJAX listener method. It should work without the throws part, as you suggest, and I've already tested it without luck. The bean is created. – Spomf Nov 27 '12 at 10:29
  • Add a value with a getter/setter, first time I see a form without values... see that post : http://stackoverflow.com/questions/9670700/fajax-listener-not-fired-for-hselectbooleancheckbox – Alexandre Lavoie Nov 27 '12 at 10:31
  • no idea... only a guess , try adding `implements Serializable` to the bean... – Daniel Nov 27 '12 at 10:34
  • @AlexandreLavoie Updated the question with a value added to the `h:selectOneMeny`. No luck. @Daniel No luck implementing `Serializable` either. – Spomf Nov 27 '12 at 10:53
  • Have you special settings inside web.xml like javax.faces.PARTIAL_STATE_SAVING set to false etc ? – Alexandre Lavoie Nov 27 '12 at 10:55
  • I have `javax.faces.PARTIAL_STATE_SAVING` set to `false`. I set it to true, but it didn't help. I also have custom values on `javax.faces.FACELETS_LIBRARIES` and `javax.faces.SEPARATOR_CHAR`, but I don't see how any of these relate to the problem. – Spomf Nov 27 '12 at 11:50
  • How did you got that code to compile? The `AjaxBehaviourEvent` class with the UK spelling doesn't exist at all. It's instead in the US spelling `AjaxBehaviorEvent`. – BalusC Nov 27 '12 at 14:32
  • @BalusC Oops, typo. I rewrote the code from scratch when I posted it here. The actual code I've tested is correct (and identical). – Spomf Nov 28 '12 at 07:57
  • @BalusC I just copied the code in the post and pasted in Netbeans to make sure they're exactly identical now. – Spomf Nov 28 '12 at 08:11
  • Kudos that you initially wrote it from top of head :) Unfortunately, the problem is not visible in the code posted so far, assuming that this code is executed in a completely blank webapp project with absolute minimum of required configuration and settings (i.e. everything is set to default as much as possible). In your case the best what I can suggest is to run a debugger and track the JSF phases. Or if you have *actually* something more into the code, then head to this answer: http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked/2120183#2120183 – BalusC Nov 28 '12 at 14:09
  • @BalusC I tried to put the code in an empty project, and it works. I'm trying to figure out what difference breakes the code in the real project. You suggest that I track the JSF phases. I know about the PhaseListener interface, and I've successfully registered a PhaseListener, but then what do I do? – Spomf Dec 03 '12 at 11:00
  • Well, if you already have no idea how to debug, then best what we can suggest is to expand the project with parts of the real project until the problem reoccurs (or the other way round; create a copy of the real project and remove parts until the problem disappears). – BalusC Dec 04 '12 at 11:57
  • @BalusC I'm already recreating the project. It's going to take a few hours, but so far so good... – Spomf Dec 04 '12 at 13:02

0 Answers0