0

I wanted to test if an ajaxified <p:selectOneListBox> element works fine or not. On a JSF+PrimeFaces based portlet I've created the above code. Basically, on a item selection from a <p:selectOneListBox> list, I want to update a label with the value of the previously selected item. Unfortunately, it doesn't seem to work as expected.

.xhtml side:

<form id="myform">
    <p:selectOneListbox id="myselect" value="#{bean.optionSelected}">
        <p:ajax listener="#{bean.onChange}" process="myselect" update="toupdate" onstart="onstart()" oncomplete="oncomplete()" onerror="onerror()" onsuccess="onsuccess()"/>
        <f:selectItem itemLabel="Option 1" itemValue="1" />
        <f:selectItem itemLabel="Option 2" itemValue="2" />
        <f:selectItem itemLabel="Option 3" itemValue="3" />
    </p:selectOneListbox>

    <h:outputText id="toupdate" value=">#{bean.optionSelected}" />
</form>

Bean side:

@ManagedBean(name = "bean")
@ViewScoped
public class Bean implements Serializable {
    private static final long serialVersionUID = 201709131528L;
    private static Logger logger = Logger.getLogger(Bean.class);

    private String optionSelected = "-1";

    public Bean() {

        logger.trace("bean created");
    }

    @PostConstruct
    private void onPostConstruct() {
        logger.trace("start");
    }

    public String getOptionSelected() {
        return this.optionSelected;
    }

    public void setOptionSelected(String optionSelected) {
        logger.trace("start");
        this.optionSelected = optionSelected;
    }

    public void onChange() {
        logger.trace("start");
    }
}

Every time I select an option from the list I've got the following log from the console:

Bean:<init>():bean created
Bean:onPostConstruct():start    

And from the console of my browser I've got the following log:

onstart():
onerror():
oncomplete():

As a newcomer to the JSF+PF world, I'd like to know: 1) why the onChange() listener is not invoked. 2) why the property optionSelected is not set. 3) why the bean bean is created on each option selection. 4) why the ajax request fails.

Any clarification would be really apreciated. Thanks.

txapeldot
  • 71
  • 2
  • 10
  • check all these: https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value I'm inclined to mark it as a duplicate but won't immediately. Sure the xhtml is an [mcve] as you actually run it? Sure the form tag is not prepended with something? And what is your JSF version? If you are on 2.2 or up, please use a more modern tutorial that uses CDI instead of JSF managed beans (the latter are deprecated in JSF 2.3) – Kukeltje Jan 09 '18 at 20:03
  • If `
    ` is not a typo, see point 1 in the link in the previous comment
    – Kukeltje Jan 09 '18 at 20:13
  • Fortunately for me, it wasn't a typo. Thanks for your help and guidance. But, looking at the `` element, how can I know what event is causing the ajax request? How can I know if it was _valuechange_ event or any other? – txapeldot Jan 10 '18 at 08:59
  • Can you create a new question for this... I'm more than happy to answer it. – Kukeltje Jan 10 '18 at 10:44
  • Question created. Thanks in advance. https://stackoverflow.com/questions/48190929/ajax-request-sent-without-defining-the-triggered-event – txapeldot Jan 10 '18 at 15:35

1 Answers1

0

As a newcomer to the JSF+PF world, I'd like to know: 1) why the onChange() listener is not invoked. 2) why the property optionSelected is not set. 3) why the bean bean is created on each option selection. 4) why the ajax request fails.

I believe all of your issues are caused by the fact that you are using an html <form> element rather than a JSF <h:form> component. I was able to reproduce all these errors locally on my machine (except #4 for some reason), and once I changed the <form> into an <h:form>, everything worked correctly.

For more information on what the JSF h:form tag does, check out these links:

stiemannkj1
  • 4,418
  • 3
  • 25
  • 45