Conditional re-rendering in JSF (Richfaces)



It happens that you want something to be re-rendered only if ajax-request is executed without errors. Or in other words – you want to be lazy if it possible and not perform “reRender” if you could. Just like that funny guy – he have to be lazy to be strong 🙂
For example you have a dialog which call some ajax-action and you want to re-render it only if ajaxrequest is executed without errors
if yo’ll put a plain it will always reRender the “list” – even if ajax request has errors.


<a4j:commandLink action="#{actionBean.action}" reRender="list" value="Run"
onComplete="if (!hasAjaxErrors()){closeModalPanel();}"/>

the workaround could be moving the reRendering to a separate a4j:jsFunction call and call it only when you want it


<a4j:commandLink action="#{actionBean.action}" value="Run
onComplete="if (!hasAjaxErrors()){closeModalPanel();onCompleteFunction()}"/>

<a4j:jsFunction name="onCompleteFunction" reRender="list" ajaxSingle="true"/>

pretty easy and your “list” will be reRendered only after success call to the “#{actionBean.action}”

XHTML code to inject the error-severity in the input field “requestSeverity”

    <a4j:outputPanel ajaxRendered="true" id="severityPanel">
        <h:form prependId="false" style="display:none" >
            <h:inputHidden id="requestSeverity"
value="#{facesContext.maximumSeverity.ordinal}"/>
        </h:form>
    </a4j:outputPanel>

Javascript function to check presence of errors (error-messages-severity)

function hasAjaxErrors() {
    var errorSeverityElement = document.getElementById("requestSeverity");
    if (errorSeverityElement){
        return errorSeverityElement.value >= 2;
    }
    return false;
}
Advertisements

https://www.facebook.com/achorniy

Tagged with: , , , , ,
Posted in Software Development, Tips and Tricks
4 comments on “Conditional re-rendering in JSF (Richfaces)
  1. josh says:

    How do you obtain the ‘hasNoAjaxErrors’ variable?

  2. Venkata Sai says:

    Fantastic Andrey Chorniy, The above example helped me a lot !!! Thanks again 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: