0

Here is a test case of my problem:

TestBean:

@ManagedBean
@ViewScoped
public class TestBean implements Serializable {
    private static final long serialVersionUID = -2329929006490721388L;

    private List<TestObject> testObjects;
    private int selectedId;

    public TestBean(){
        List<TestObject> to = new ArrayList<TestObject>();
        for(int i=0; i<10; i++){
            TestObject o = new TestObject(i, "object-"+i);
            to.add(o);
        }
        this.setTestObjects(to);
    }

    public void testAjaxListener(int id){
        System.out.println("testAjaxListener("+id+")");
        this.setSelectedId(id);
    }

    //+getters/setters
}

TestObject

public class TestObject {
    private int id;
    private String name;

    public TestObject(int id, String name){
        this.setId(id);
        this.setName(name);
    }

    //+getters/setters
}

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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>
    <h:form id="testForm">
        <h:panelGroup rendered="#{param['view'] eq 'test'}">
            <h2>DataTable</h2>
            <h:dataTable var="o" value="#{testBean.testObjects}">
                <h:column>
                    <h:commandLink value="#{o.name}" actionListener="#{testBean.testAjaxListener(o.id)}">
                        <f:ajax
                            render=":testForm:outputTest"
                        />
                    </h:commandLink>
                </h:column>
            </h:dataTable>
            <h2>output</h2>
            <h:outputText id="outputTest" value="#{testBean.selectedId}" />
        </h:panelGroup>
    </h:form>
</h:body>
</html>

The problem is, that actionListener won't firing (i'm checking that with System.out.print as you can see). It works fine when i remove conditional render from panelGroup, so i think that is the issue - but how can i fix it? I have readed those topics: h:commandLink / h:commandButton is not being invoked, f:ajax inside conditionally rendered custom tag - backing bean method not invoked and many more, but it didn't solve my problem :(

Please help

0

1 Answer 1

0

It's because the #{param['view'] eq 'test'} didn't evaluate true while JSF is busy processing the ajax submit. It will then also consult the rendered, disabled and readonly attributes once again as safeguard against hacked requests. JSF namely doesn't include the request parameters in the <form action> URL as generated by <h:form>. This matches point 5 of commandButton/commandLink/ajax action/listener method not invoked or input value not updated.

There are several ways to get around this.

  1. Set it as a property of the view scoped bean via <f:viewParam>.

    <f:metadata>
        <f:viewParam name="view" value="#{testBean.view}" />
    </f:metadata>
    ...
    <h:panelGroup rendered="#{testBean.view eq 'test'}">
    
  2. Manually retain the param via <f:param> (you need to put it in every submit action!):

    <h:commandLink ...>
        <f:param name="view" value="#{param.view}" />
        ...
    </h:commandLink>
    
  3. Replace <h:form> by OmniFaces <o:form> which is capable of telling JSF that it must submit the form to an URL including the request parameters:

    <o:form includeRequestParams="true">
        ...
    </o:form>
    

See also:

2
  • Thanks for help! :D Point 2 has solved my problem. Unfortunately, points 1 and 3 didn't work for some reason:/ Sep 6, 2014 at 15:02
  • I was wrong. OmniFaces form also helped;-) Sep 6, 2014 at 15:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.