1

I'm trying to invoke a <f:ajax listener> method on change of <h:selectOneMenu>.

This is the view:

<h:selectOneMenu label="verteilerlisten" 
    value="#{smsBean.verteilerliste}" id="verteilerlisten">
    <f:ajax listener="#{smsBean.verteilerlistenChanged}" 
        render="verteilerlisten" />
    <f:selectItems value="#{verteilerlisten.list}" 
        var="v" itemLabel="#{v.name}" itemValue="#{v.verteilerlistennummer}" />
</h:selectOneMenu>

This is the #{smsBean}:

public void verteilerlistenChanged(AjaxBehaviorEvent e) {
    System.out.println("success!");
    throw new RuntimeException("Success!");
}

The items are displayed correctly, but nothing happens when I change the item in the select box. How is this caused and how can I solve it?

EDITED:

I created a blank xhtml file (test.xhtml) with the following code:

<?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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head />
<h:body>
    <h:form id="smsform">
        <h:selectOneMenu label="verteilerlisten" value="#{smsBean.verteilerliste}"
                         id="verteilerlisten">
            <f:selectItems value="#{verteilerlistenBean.list}" var="v"
                           itemLabel="#{v.name}" itemValue="#{v.verteilerlistennummer}"/>
            <f:ajax listener="#{smsBean.verteilerlistenChanged}" render="smsform"/>
        </h:selectOneMenu>
    </h:form>
</h:body>
</html>

This is the VerteilerlistenBean.java:

@ManagedBean(name= "verteilerlistenBean")
@ApplicationScoped
public class VerteilerlistenBean {
  private Logger logger = Logger.getLogger(VerteilerlistenBean.class);
  private List<Verteilerliste> list = new ArrayList<Verteilerliste>();

  public VerteilerlistenBean() {
  }

  public List<Verteilerliste> getList() {
    // return list;
    logger.info("Getter invoked");

    List<Verteilerliste> list = new ArrayList<Verteilerliste>();

    list.add(new Verteilerliste(1, "Liste 1"));
    list.add(new Verteilerliste(2, "Liste 2"));
    list.add(new Verteilerliste(3, "Liste 3"));
    list.add(new Verteilerliste(4, "Liste 4"));
    list.add(new Verteilerliste(5, "Liste 5"));

    return list;
  }

  public void setList(final List<Verteilerliste> parList) {
    list = parList;
  }
}

And this is the smsBean (SendSMSFormBean.java):

@ManagedBean (name="smsBean")
@ViewScoped
public class SendSMSFormBean implements Serializable {
  private String absender;
  private Verteilerliste verteilerliste;
  private Textbaustein textbaustein;

  public SendSMSFormBean() {

  }

  public String getAbsender() {
    return absender;
  }

  public void setAbsender(final String parAbsender) {
    absender = parAbsender;
  }

  public Verteilerliste getVerteilerliste() {
    return verteilerliste;
  }

  public void setVerteilerliste(final Verteilerliste parVerteilerliste) {
    verteilerliste = parVerteilerliste;
  }

  public Textbaustein getTextbaustein() {
    return textbaustein;
  }

  public void setTextbaustein(final Textbaustein parTextbaustein) {
    textbaustein = parTextbaustein;
  }

  public void verteilerlistenChanged(AjaxBehaviorEvent e) {
    System.out.println("Success!");
  }
}

These are my libs I use:

(I can't upload image -.- )

  • javassist-3.15.0-GA.jar
  • javax.faces-2.0.11.jar
  • javax.servlet-5.1.12.jar
  • javax.servlet-api-3.0.1.jar
  • javax.servlet.jar
  • javax.servlet.jsp.jstl-1.2.1.jar

EDITED 2

I cleaned my classpath lib so that all these classes aren't inside there anymore (it uses the libs from tomcat now). I also added a in the plain example (i got a in the main example). But still everytime I change the value in the selectBox in the console appears:

2014-01-21 16:03:48 INFO  VerteilerlistenBean:44 - Getter invoked

EDITED 3

When changing f:ajax listener attribute to "#{asdf.asdf}. No error was shown! Before that I tried #{smsBean.verteilerlistenChanged} and ${smsBean.verteilerlistenChanged}

EDITED 4 So now I think I got the mistake:

<div class="ym-fbox">
<h:selectOneMenu label="verteilerlisten" id="verteilerlisten">
    <f:selectItems value="#{testbean.list}" var="v" itemLabel="#{v.name}" itemValue="#{v.id}"/>
    <f:selectItem itemLabel="Manuelle Eingabe" itemValue="man"/>
    <f:ajax listener="#{testbean.beep}" render="smsform"/>
</h:selectOneMenu>
</div>


<div class="ym-fbox">
<h:selectOneMenu label="verteilerlisten1" id="verteilerlisten1" value="#{smsBean.verteilerliste}">
    <f:selectItems value="#{verteilerlistenBean.verteilerlisten}" var="liste" itemLabel="#{liste.name}" itemValue="#{liste.verteilerlistennummer}" />
    <f:selectItem itemLabel="Manuelle Eingabe" itemValue="man"/>
    <f:ajax listener="#{testbean.beep}" render="smsform"/>
</h:selectOneMenu>
</div>

The upper one invokes testbean.beep when changed the other one doesn't! It would work if you remove attribute "value" in selectOneMenu. I don't know why JSF does this or why it doesn't show me any error..

But that doesn't solve my problem: I have 2 Beans. One is a Bean, that safes the inputs in the form (smsBean). The select Value should be saved in this bean. The other bean is something like a RessourceManager, which returns the values that the user can select in the box.

How do I make this?

  • 1
    To avoid the obvious, is this placed inside a form? In any case, just work through all probable causes listed here http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked – BalusC Jan 21 '14 at 13:13
  • Thank you for the link! I checked all points but nothing helped me.. Yes it is in a form. I did some tests and saw, that everytime when I change the value in the Box, the getter of smsBean.verteilerlisten is invoked. I also changed the event to "change", "valueChange" and blank, but in no case it worked... Do you have any other ideas? – Maximilian Schmidt Jan 21 '14 at 13:54
  • Create an SSCCE. Code given so far works just fine for me in a completely blank JSF project with latest versions and everything set to default. – BalusC Jan 21 '14 at 14:04
  • Your SSCCE conflicts #6 of the given list. Where's the ``? By the way, I hope you don't have those `javax.servlet.*` JARs in your runtime classpath..?? See also among others http://stackoverflow.com/questions/4076601/how-do-i-import-the-javax-servlet-api-in-my-eclipse-project/ – BalusC Jan 21 '14 at 14:20
  • What is an SSCCE? I updated the main post and added some further information.. – Maximilian Schmidt Jan 21 '14 at 14:21
  • You already posted one. In any way, SSCCE is explained in 1st google hit on that term. – BalusC Jan 21 '14 at 14:21
  • sry for that, I am new in that stuff, thank you for the link! But it still doesn't work.. (see EDITED 2) – Maximilian Schmidt Jan 21 '14 at 15:09
  • Anything odd in JS console or HTTP traffic monitor? – BalusC Jan 21 '14 at 15:11
  • @BalusC It seems to be fine.. Maybe this could help: I change the attribute listener in f:ajax to #{adsf.asdf} and it displayed me NO error, that this bean doesn't exist. When I change the value of the selectBox it still displays "Getter invoked" in the console.. – Maximilian Schmidt Jan 21 '14 at 15:17
  • Then I don't know. At least, those container-specific JARs in your webapp's lib are fishy. I suggest to throw away everything and create the project from scratch based on a sane book/tutorial/resource. – BalusC Jan 21 '14 at 16:35
  • @BalusC I think I got it, it seems like when the selectOneMenu has a attribute value it doesn't work... (see Edited) – Maximilian Schmidt Jan 22 '14 at 12:35
  • Have you read point #3 of the link in my 1st comment? Big chance that you actually have "Validation error: Value is not valid". – BalusC Jan 22 '14 at 12:47
  • Okay I finally got it! I had to write a Converter for object Verteilerliste. Thank you very much for you help!! – Maximilian Schmidt Jan 22 '14 at 13:26

0 Answers0