2013-02-20 30 views
0

我有這樣的事情:我怎麼可以導航到一個絕對路徑從啊:的commandButton

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

一旦操作方法進行評估,我想導航(僅在某些情況下),以絕對URL 。

我知道如果「bean.action」返回一個與上下文中有效路徑對應的相對路徑,它將會去那裏,但我不想那樣做。

我要評估行動的回報,如果它都可以瀏覽到一個絕對的URL,如「www.anypage.com」,像

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

但取決於行動回報,並保持優勢JSF2 。

任何想法? THX很多

+0

的重複:http://stackoverflow.com/questions/7159358/hcommandbutton-how-to-redirect-to-external- sitejsf-2,絕對網址也被稱爲外部網址。 – 2013-02-20 12:39:44

+0

'www.anypage.com'根本不是絕對的URL。它並不是從一個計劃開始的。如果當前位於'http:// example.com/context/somepage.xhtml'中,那麼在點擊該鏈接後,您最終會在'http:// example.com/context/www.anypage.com'中結束,這很可能會導致在404錯誤。 – BalusC 2013-02-20 13:23:15

+0

我會考慮它,thx很多 – davidml 2013-02-21 11:18:57

回答

2

很簡單,只需在您的操作方法使用ExternalContext.redirect()

public String action() { 
    doYourProcessing(); 
    if(/*some condition*/) { 
     FacesContext.getCurrentInstance().getExternalContext().redirect(/*absolute path*/); 
    } else { 
     //return a navigation case outcome or null for a postback 
    } 
} 
相關問題