I've been dealing with a problem with JSF, when it comes to redirect to pages inside my app it works just fine, but I haven't been able to redirect to external URL can some one guide me on this?
Asked
Active
Viewed 6.4k times
41
-
What happens when you try to redirect to an external URL? How are you trying to do it? Show us some code. – Matt Ball Feb 23 '11 at 14:54
-
1@Matt: I'll bet that he was fiddling with navigation cases and outcome values. That's indeed not possible then. For internal pages a `
` or `outcome?faces-redirect=true` works fine. – BalusC Feb 23 '11 at 14:57
1 Answers
103
Either just mention the URL directly in <a>
or <h:outputLink>
.
<a href="https://stackoverflow.com">Go to this site!</a>
<!-- or -->
<h:outputLink value="https://stackoverflow.com">Go to this site!</h:outputLink>
Or, if you need to to invoke a bean action using <h:commandLink>
like below,
<h:form>
<h:commandLink value="Go to this site!" action="#{bean.redirect}" />
</h:form>
then use ExternalContext#redirect()
in action method.
public void redirect() throws IOException {
// ...
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect("https://stackoverflow.com");
}
Note that you don't need to catch that IOException
, the server will deal with it. Also note the importance of including the scheme (https://
or http://
or //
) in the URL, otherwise it will be interpreted relative to the current domain.

BalusC
- 1,082,665
- 372
- 3,610
- 3,555
-
thanks balus the ExternalContext solution works, proable i was not putting the "http" prefix on my URL, +1 for quickily and certain answer AGAIN – Necronet Feb 23 '11 at 15:12