Redirect to external URL in JSF

試著忘記壹切 提交于 2019-11-26 04:25:19

问题


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?


回答1:


Either just mention the URL directly in <a> or <h:outputLink>.

<a href="http://stackoverflow.com">Go to this site!</a>
<!-- or -->
<h:outputLink value="http://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("http://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 (http:// or https:// or //) in the URL, otherwise it will be interpreted relative to the current domain.



来源:https://stackoverflow.com/questions/5092439/redirect-to-external-url-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!