webtier@glassfish.java.net

f:ajax and getRenderResponse

From: <webtier_at_javadesktop.org>
Date: Fri, 16 Oct 2009 13:40:31 PDT

Hey folks, I just wanted to run something by you to see if it is a problem or not. I've been playing with the f:ajax tag and come across what might be an issue.

In my page, when I render it, I call a getResults() method which checks the value of FacesContext.getCurrentInstance().getRenderResponse to see if it has been set. In a normal JSF cycle, half way through, this value would change to true once that phase has started. With ajax, it seems things are sometimes different :

If the ajax tag is inside a command button,

            <h:commandButton value="Search">
                <f:ajax render="results" execute="expression"/>
            </h:commandButton>

Everything works fine, the page is re-rendered and half way though my getRenderResponse flag changes to true once rendering is started.

However, if the ajax tag is inside an input control :

<h:inputText value="#{searchBean.expression}" id="expression">
    <f:ajax event="keydown" render="results" execute="expression"/>
</h:inputText>

In this case, the page is re-rendered (the content of the page actually changes) but my render response flag is always set to false.
I don't know whether this is intentional, when the ajax request comes from a UI input control as opposed to a command control, but I thought I would check before I file a bug report.

Here is my test case :


        <h:form id="form">

            Expression : <h:inputText value="#{searchBean.expression}" id="expression">
                                <f:ajax event="keydown" render="results"/>
            </h:inputText>

            <h:dataTable value="#{searchBean.results}" var="v_row" id="results">
                <h:column><f:facet name="header">value</f:facet> #{v_row}</h:column>
                
            </h:dataTable>

            <h:commandButton value="Search">
                <f:ajax render="results" execute="expression"/>
            </h:commandButton>
        </h:form>

My Bean :


@ManagedBean(name = "searchBean")
@RequestScoped
public class SearchBean {

    private String expression;

    public String getExpression() {
        return expression;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }

    public List<String> getResults() {
        boolean flag = FacesContext.getCurrentInstance().getRenderResponse();
        System.out.println("Render Response = "+ flag);
        return generateResults();
    }

    public List<String> generateResults() {

        List<String> results = new ArrayList<String>();
        if (expression != null) {
            results.add(expression + " - line 1");
            results.add(expression + " - line 2");
            results.add(expression + " - line 3");
        }
        return results;
    }
}

In this example, clicking the button will send the expression and update the results and the render response will change to true.

If you edit the expression, it will refresh as you type, but the render response value stays false.

The reason this is relevant if for lazy loading data sets only at render time as opposed to also during the apply model values. This is so I only need to load the data once but since the render response flag is never being set, it isn't loading any data. If it is a problem, let me know and I'll file a report.

Cheers,

Andy Gibson
[Message sent by forum member 'agibson77' (contact_at_andygibson.net)]

http://forums.java.net/jive/thread.jspa?messageID=368273