4 Replies Latest reply: Jul 6, 2012 9:43 AM by David Baddeley RSS

JSF View ID expires on AJAX request

David Baddeley Newbie

Hi All,

 

I am a JSF/richfaces beginner so please bear with me on this one...

 

I am loading an xhtml page via ajax using the jQuery.load() method, the page contains an <a4j:form> and <a4j:jsFunction> which is fired when the page has fully loaded, this all works ok

 

The problem I have found is that after this AJAX request has finished if I then try to execute any further <a4j:Function>'s from the original page I get the JSF View ID Expired exception (

JSF1054: (Phase ID: RESTORE_VIEW 1, View ID: )

 

So it seems that the <a4j:jsFunction> that is within the dynamically loaded page is causing the JSF View ID to expire for all other AJAX requests

 

Can anybody help me out with this one as im sure there must be a way to prevent this from happening??

 

Thanks

 

Dave

  • 1. Re: JSF View ID expires on AJAX request
    Brendan Healey Master

    This is a non-starter I'm afraid because JSF/Richfaces is a server-side technology. The server sends pages to the client but

    it stores the state in the component tree, so that when the client sends requests back to the server the server knows about

    the session the request belongs to, which component etc...

     

    If you try using jquery ajax to load some xhtml then the server just isn't going to play ball. I guess you're trying to use

    a4j:jsFunction to send data to and receive data back from the server. I think that if you're going to use jquery ajax functions

    you'll need to use a servlet on the server side along with a JSON library to handle the client requests, and I've often been

    tempted to do this because it gives you access to some very good jquery plugins.

     

    Regards,

    Brendan.

  • 2. Re: JSF View ID expires on AJAX request
    David Baddeley Newbie

    Hi Brendan, thanks for the quick reply

     

    I see what your saying, I have used the jQuery.load() method quite a few times within the same application but I guess what your saying is that the component tree is being reset by the jQuery AJAX request?

     

    Is there a richfaces/JSF (out of the box) way of dynamically loading a .xhtml pages that have further <a4j:jsFunction>'s within them?? The servlet method seems a bit much for what I need

     

    Cheers

    Dave

  • 3. Re: JSF View ID expires on AJAX request
    Brendan Healey Master

    >I see what your saying, I have used the jQuery.load() method quite a few times within the same application but I guess

    >what your saying is that the component tree is being reset by the jQuery AJAX request?

     

    Well, I'm saying that the server is expecting to get requests from client pages that it has served (clearly there's a bootstrap

    scenario here but even so). The best thing to do is to look at a POST request using a decent browser (not IE8 or below) in

    the developer tools/firebug.

     

    So put a <a4j:commandButton>  on a page and click it. In the chrome browser developer tools click the network tab and

    then click on the post request. In the Headers tab look at the post form data. Amongst other things you'll see

    javax.faces.ViewState. Compare the content of this request with what you're sending from jquery and you'll have a better

    idea of what's going on.

     

    >Is there a richfaces/JSF (out of the box) way of dynamically loading a .xhtml pages that have further <a4j:jsFunction>'s

    >within them?? The servlet method seems a bit much for what I need

     

    The only way to do this as far as I'm presently aware is to turn off partial state saving on a view (or completely) , then you

    can use ui:include with an EL expression for the src= attribute and I definitely did get this to work at one point. The

    problem was that I had problems with richfaces when using full state saving, although I can't recall exactly what didn't work

    and this was with a much earlier release of RF4. You can turn off partial state saving (or turn on full state saving) for a

    specific view or set of views (comma separated I think) with this in web.xml:

     

    <context-param>

            <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>

            <param-value>/myPage.xhtml</param-value>

    </context-param>

     

    Generally you can get dynamic content to display ok. The real test is when you have UICommand components

    on the page. You often find that a commandLink/Button, won't work at all, they only work on the first click, won't work on

    the first click etc....

     

    If anyone knows any better than I regarding this subject then I'd be delighted to see a small sample of code that

    dynamically loads some content containing a commandButton/Link that works first click, second click and every other

    click all the time!

     

    Regards,

    Brendan.

  • 4. Re: JSF View ID expires on AJAX request
    David Baddeley Newbie

    Ok I tried your soultion but using the <a4j:include> component which seems to do the job (I am on RF 3.3.3), but ran into a new problem as I am using inline javascript to declare JS variables from my backing bean, such as...

     

    <s:div rendered="#{!empty sessionProcessingActions.selectedMpd}">

      <script type="text/javascript">

             //<![CDATA[

    var sessionXml = jQuery('#{sessionProcessingActions.selectedMpd.sessionXml}');

             var starts = #{sessionProcessingActions.selectedMpd.correlatedStartTimes};

             var ends = #{sessionProcessingActions.selectedMpd.correlatedEndTimes};

             //]]>

      </script>

    </s:div>

     

    Unfortunatly though, the browser doesnt render the script tags whatsoever (security feature?)

     

    So I have reverted to moving all of the a4j components back into the parent .xhtml page and using jQuery.load() to just load the plain html elements, css and JS which seems to work ok

     

    Would be interested to hear if there are any better suggestions though