Question

I've got a dropdown on my index.xhtml JSF front page. The associated code/commandButton looks like this:

    <h:selectOneMenu id="nodes" value="#{MyBacking.chosenNode}">
    <f:selectItems value="#{MyBacking.nodes}" />
</h:selectOneMenu>

<a4j:commandButton value="Retrieve" styleClass="ctrlBtn"
    id="retrieveBtn" style="margin-bottom: 2px;"
    onclick="#{rich:component('nodeLoadWait')}.show()"   # modal
    action="#{MyBacking.redirect}"
    image="/img/btnRetrieve26.png" />

action was set to 'hello' previously, and in my faces-context.xml:

<navigation-rule>
    <from-view-id>/index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>hello</from-outcome>
        <to-view-id>/nodeMgmt.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

When action was set to 'hello', clicking the retrieve button worked OK in that faces would handle the nav and MyBacking.setChosenNode method would assign all the necessary data, so that the content of nodeMgmt.xhtml would be displayed fully populated.

However, if the initial activity caused by the user clicking retrieve times out, the web page would hang even though the bean detects the time out, and I'd like to redirect the user to a 'timed out' page.

In order to handle the backing bean returning a timedout message (the error detection for which is already present when 'inside' the app), I thought rather than using the faces-context.xml file, I would handle it internally.

I found FacesContext.getCurrentInstance().getExternalContext().redirect but the JSF 1.2 javadoc does not feature this. Perhaps it's because it's not featured? It redirects though which is confusing. Why no documentation on this method?

Nonetheless, it redirects me to the page, but renders without taking into account the data instantiated by the bean during the initial request. The bean is in request scope currently. The relevant code in the bean is

    try {
        FacesContext.getCurrentInstance().getExternalContext().redirect("nodeMgmt.jsf");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Is using a backend java call the best way to do this kind of redirection? If not, is it best to use faces-context.xml? If so, how? While we're here - can anyone direct me to a good reading resource for FacesContext.getCurrentInstance().getExternalContext() usage which has decent examples about how to do simple navigation with data cos I'm having trouble locating one.

Cheers

Was it helpful?

Solution

I found FacesContext.getCurrentInstance().getExternalContext().redirect but the JSF 1.2 javadoc does not feature this. Perhaps it's because it's not featured? It redirects though which is confusing. Why no documentation on this method?

There certainly is.

Probably you was reading the wrong javadoc. The one of FacesContext perhaps?


Nonetheless, it redirects me to the page, but renders without taking into account the data instantiated by the bean during the initial request. The bean is in request scope currently.

A redirect instructs the browser to fire a brand new HTTP request. So all request scoped beans from the old request will be garbaged and reinitialized in the new request. If you'd like to retain the request, you'd like to use a forward instead (which JSF by default uses), but this isn't going to work on ajax-initiated requests as it will stick to the same page anyway. Only a response with a redirect will force Ajax to change the window location using JavaScript.

If you want to retain some parameters in the new request, you'd have to pass them as request parameters. E.g.

externalContext.redirect("nodeMgmt.jsf?foo=bar");

and set them as managed property in the bean.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top