dev@jsftemplating.java.net

naviage vs redirect

From: Anissa Lam <Anissa.Lam_at_Sun.COM>
Date: Wed, 11 Oct 2006 15:30:19 -0700

Hi,
I just want to share with you something that i observed with using
navigate() and redirect() handler. They behave quite differently and
you may want to decide which is the right one for you when you need to
go to another page.
Here is what i observed:

- redirect() :
    the attribute you set before calling this will *NOT* be available at
the new page
    the request parameter will be available though
    the page parameter that you specify has to be relative from the page
you are on. You can't use absolute path or assuming it starts from
context root.

- navigate(): just the opposite of redirect,
    - the request attribute will be available
    - the request parameter will *NOT* be available.
    - if you get to this page via a navigate, any attribute you set
during a <!beforeCreate> of the page will lost before it even get to the
<!beforeEncode>. To avoid this problem, try to set your attributes in
<!beforeEncode> unless you absolutely need that for creating the
components.
   - the page parameter is from context root.

I spent quite some time playing with these in order to be able to show
errors in admingui.
I have attached a testNavigate.jsf that shows you the above findings.

thanks
Anissa.




<sun:page id="page1">
    <!beforeCreate
        setPageSessionAttribute(key="nextPage" value="testNavigate.jsf");
        println(value="before Create : nextPage = '#{nextPage}', errorSummary = '#{errorSummary}'" );
    />
    
    <!beforeEncode
       println(value="before Encode : nextPage = '#{nextPage}', errorSummary = '#{errorSummary}'" );
   />
   
    <sun:html id="html2">
    
    <sun:alert id="pageAlert" type="error" summary="#{errorSummary}" /> "<br /><br />
    
        <sun:staticText text="Request Parameter foo ='$requestParameter{foo}'" /> "<br /><br />
        <sun:form id="propertyForm">
            <sun:button text="Navigate, nextPage=#{nextPage} ,error attr persist but request param is lost" id="navigate" >
            <!command
                println(value="Using Navigate");
                setAttribute(key="errorSummary" value="Since you come here using Navigate, Error is going to occur no matter which button you select. nextPage attribute is lost.");
                navigate(page="#{nextPage}?foo=BAR");
                />
            </sun:button>

            "<br /><br />

             <sun:button text = "Redirect nextPage=#{nextPage}, error attr is lost, but request param is there" >
            <!command
                println( value="Using Redirect");
                setAttribute(key="errorSummary" value="summary before Redirect, you will never see this !!! ");
                redirect(page="#{nextPage}?foo=BAR");
                />
            </sun:button>

        </sun:form>
    </sun:html>
</sun:page>