You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Dean Hiller <de...@xsoftware.biz> on 2006/03/03 13:25:48 UTC

return url from action method, OR other option....

I have an action method

public String submit() {
    return "success";
}


but, unfortunately, I have to query another service for the URL to 
really redirect to!!!  This means, I can't put the URL in the 
faces-config.xml file.  Is there any way to do any of these options(or 
others....list is not exhaustive probably)

1. return a URL from action methods like http://xxxxx.com
2. modify the faces-config dynamically like changing a mapping for 
"success" and then returning "success" in the submit method.
3. interact with ServletRequest and tell it to redirect(though I don't 
want to depend on servlets and would like to be portable between 
portlets and servlets maybe....I know nothing about portlets so don't 
know if portability is useful or not yet)
4. add a servlet filter somehow such that it can look at the url 
returned from the submit method?

How in the world do I do this?  I have an api

public URL getApplicationPage(int appId);

that I get the url from.

PLEASE TALK SLOW, as I am quite new to JSF, servlet filters(I have only 
used servlets really and seen filters once in a while).

thanks,
dean


Re: return url from action method, OR other option....

Posted by Laurie Harper <la...@holoweb.net>.
Mario Ivankovits wrote:
> Hi Dean!
>> but, unfortunately, I have to query another service for the URL to
>> really redirect to!!!  This means, I can't put the URL in the
>> faces-config.xml file.  Is there any way to do any of these options(or
>> others....list is not exhaustive probably)
> I am not definitely sure, but maybe a navigation handler could help.
> 
> *) create a class which extends from
> "javax.faces.application.NavigationHandler"
> *) create a constructor which takes one parameter which is also a
> NavigationHandler - we call it originalHandler
> *) override handleNavigation
> *) configure this class in your faces-config.xml
> *) if the fromAction is a url (e.g. starts with http://) get the
> externalContext/response object - cast it to httpServletresponse and
> *) call sendRedirect (or so, cant remember now)
> *) else call originalHandler.handleNavigation

Note that you can achieve the same thing in your backing bean action, 
without having to install a view handler. The trick is to tell JSF 
you've handled the response so it skips generating one. Something like this:

   public String myAction() {
     String url = getMyUrl();
     FacesContext ctx = FacesContext.getCurrentInstance();
     ctx.getExternalContext().redirect(url);
     ctx.responseComplete();
     return null;
   }

L.


Re: return url from action method, OR other option....

Posted by Mario Ivankovits <ma...@ops.co.at>.
Hi Dean!
> but, unfortunately, I have to query another service for the URL to
> really redirect to!!!  This means, I can't put the URL in the
> faces-config.xml file.  Is there any way to do any of these options(or
> others....list is not exhaustive probably)
I am not definitely sure, but maybe a navigation handler could help.

*) create a class which extends from
"javax.faces.application.NavigationHandler"
*) create a constructor which takes one parameter which is also a
NavigationHandler - we call it originalHandler
*) override handleNavigation
*) configure this class in your faces-config.xml
*) if the fromAction is a url (e.g. starts with http://) get the
externalContext/response object - cast it to httpServletresponse and
*) call sendRedirect (or so, cant remember now)
*) else call originalHandler.handleNavigation

> 1. return a URL from action methods like http://xxxxx.com
this is required for the above
> 2. modify the faces-config dynamically like changing a mapping for
> "success" and then returning "success" in the submit method.
Not possible. myFaces wont reload it, the best you can archive here is
to reload the whole application - for sure not what you want ;-)
> 3. interact with ServletRequest and tell it to redirect(though I don't
> want to depend on servlets and would like to be portable between
> portlets and servlets maybe....I know nothing about portlets so don't
> know if portability is useful or not yet)
This is what the above does. But for sure, not portlet compatible. But I
think the requirement itself isnt protlet compatible. What should a
protlet engine do if you direct the browser to do a redirect.
> 4. add a servlet filter somehow such that it can look at the url
> returned from the submit method?
not possible.

So, now dig into JSF an try it ;-)

Ciao,
Mario