Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

Redirect to external URL in JSF

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...