0

I have something like this:

<h:commandButton value="test" action="#{bean.action}"/>

once the action method is evaluated, I would like to navigate (only in some cases) to an absolute URL.

I know that if the "bean.action" returns a relative path that corresponds to a valid path in context it will go there, but I don't want that.

I want to evaluate the action return and if it is all ok navigate to an absolute URL like "www.anypage.com", something like

<a href="www.anypage.com">test</a>

but depending on action return and keeping the jsf2 advantages.

Any ideas? Thx a lot

davidml
  • 153
  • 5
  • 20
  • Duplicate of: http://stackoverflow.com/questions/7159358/hcommandbutton-how-to-redirect-to-external-sitejsf-2, an absolute URL is also called an external url. – Christophe Roussy Feb 20 '13 at 12:39
  • `www.anypage.com` is not an absolute URL at all. It does not start with a scheme. When currently in `http://example.com/context/somepage.xhtml`, you'd after clicking that link end up in `http://example.com/context/www.anypage.com` which will likely result in a 404 error. – BalusC Feb 20 '13 at 13:23
  • I will consider it, thx a lot – davidml Feb 21 '13 at 11:18

1 Answers1

2

Easy, just use ExternalContext.redirect() in your action method:

public String action() {
    doYourProcessing();
    if(/*some condition*/) {
        FacesContext.getCurrentInstance().getExternalContext().redirect(/*absolute path*/);
    } else {
        //return a navigation case outcome or null for a postback
    }
}
skuntsel
  • 11,624
  • 11
  • 44
  • 67