2012-05-06 97 views
1

Im有一些重定向問題。我使用primefaces和JSF 2.1。事情是,JSF 2.1沒有導航規則,並尋找答案,我發現我可以把「faces-redirect = true」。問題是,這沒有奏效,我不知道爲什麼。瀏覽器總是告訴我:「我沒有導航的東西,就像我沒有導航一樣」用「/Autenticacion/login.xhtml」作爲第二個結果的第一個操作。使用JSF 2.1不會創建一個faces-config.xml文件,我創建它並添加了該操作的規則,但問題仍然存在。不重定向JavaFaces 2.0 Primefaces p:commandButton o p:commandLink

這些都是我的文件:

LOGIN BEAN

@ManagedBean(name="controlLogin") 
@SessionScoped 
public class ControladorLogin implements Serializable{ 

    public String logIn(){ 
     //actions 
     return "index" //algo tryed index.xhtml or index?faces-redirect=true 
    } 

PRIMEFACES COMMANDBUTTON

<p:commandButton action="#{controlLogin.logIn}" value="Loguearse" ajax="false"/> 

我也嘗試用commandLink

<p:commandLink action="#{controlLogin.logIn}" value="Loguearse" ajax="false"/> 

faces-config.xml中/ /如果不是必須的我可以刪除它

<navigation-rule> 
    <from-view-id>/Autenticacion/login.xhtml</from-view-id> 
    <navigation-case> 
     <from-action>#{controlLogin.logIn}</from-action> 
     <from-outcome>index</from-outcome> 
     <to-view-id>/index.xhtml</to-view-id> 
    </navigation-case> 
</navigation-rule> 

所以,如果任何人都可以幫助我做這個重定向...謝謝!

+0

嘗試使用FacesContext.getCurrentInstance()。getExternalContext()。redirect(yourURL); – rags

回答

4

確實,在JSF 2.x您不必定義導航規則。根本不需要faces-config.xml,只是在某些特殊情況下,註釋(例如,自定義ExceptionHandlerFactory)未覆蓋。

使用return "index?faces-redirect=true"是完全沒問題的。 faces-redirect=true表示您從一個xhtml到另一個而不是默認forward

Difference between forward and redirect

在這種情況下,只要確保你的index.xhtml頁在同一目錄,從中您嘗試訪問的頁面。

否則,如果index頁位於另一目錄,那麼你必須在這樣的beggining指定絕對路徑用斜槓/"/indexFolder/index?faces-redirect=true"

+0

感謝您的回答。只是爲了記錄,就像我正在使用java-faces,我不得不將「/ faces」添加到我的頁面所在的文件夾路徑。非常感謝! –

+0

只是一個猜測。你可能已經定義' \t \t 面臨的Servlet \t \t /面/ * \t'在你的'web.xml'。如果你改變(或者添加新的)'url-pattern'到' * .jsf(或* .xhtml)',它將在不添加「/ faces」到文件夾路徑的情況下工作。 – Fallup

+0

@Fallup非常真實,謝謝。 – sampathpremarathna

相關問題