2011-02-23 125 views
29

我一直在處理JSF的問題,當涉及到重定向到我的應用程序內部的頁面時,它工作得很好,但我一直無法重定向到外部URL可以一些在這方面指導我?重定向到JSF中的外部URL

+0

當您嘗試重定向到外部URL時會發生什麼?你打算怎麼做?向我們展示一些代碼。 – 2011-02-23 14:54:25

+0

@Matt:我敢打賭,他正在擺弄導航案例和結果值。那確實不可能。對於內部頁面,「」或「結果?faces-redirect = true」可以正常工作。 – BalusC 2011-02-23 14:57:06

回答

71

要麼直接在<a><h:outputLink>中提及URL。

<a href="http://stackoverflow.com">Go to this site!</a> 
<!-- or --> 
<h:outputLink value="http://stackoverflow.com">Go to this site!</h:outputLink> 

或者,如果您需要調用使用<h:commandLink>象下面這樣一個bean動作,

<h:form> 
    <h:commandLink value="Go to this site!" action="#{bean.redirect}" /> 
</h:form> 

然後在操作方法使用ExternalContext#redirect()

public void redirect() throws IOException { 
    // ... 

    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); 
    externalContext.redirect("http://stackoverflow.com"); 
} 

請注意,您不需要捕獲那個IOException,服務器將處理它。還要注意在URL中包含該方案(http://https:////)的重要性,否則將相對於當前域進行解釋。

+0

感謝Balloon ExternalContext解決方案的工作原理,我沒有在我的網址上加上「http」前綴,快速地+1並且再次給出某些答案 – Necronet 2011-02-23 15:12:43