2012-10-10 33 views
4

我認爲它是一個很好的做法,有一個索引頁面(在我的情況下index.xhtml)。 我想在索引頁面上傳遞一些動作(例如在struts中:<c:redirect url="list.do" />,我去沒有任何鏈接和按鈕的struts動作類)我知道是否要使用導航我應該使用commandLink-s或按鈕)。我可以用onclick javascript函數寫<h:commandButton>,但我不覺得這是最好的選擇。JSF index.xhtml和重定向面臨行動

我完全不熟悉JSF(使用JSF 2.0),我需要您的建議。從索引頁重定向到控制器中的操作的最佳做​​法是什麼?

///新版本

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 
<f:view> 
<ui:insert name="metadata"/> 
    <f:viewParam name="action" value="listItems.xtml"/> 
    <f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" /> 
<h:body></h:body> 
</f:view> 
</html> 

public class ForwardBean { 

    private String action; 

    // getter, setter 

    public void navigate(PhaseEvent event) { 
     FacesContext facesContext = FacesContext.getCurrentInstance(); 
     String outcome = action; 
     facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome); 
    } 
} 
+0

所以它在修改後的工作? – Swarne27

+0

爲什麼你需要這個? – Swarne27

+0

只是在我的解決方案中刪除「list.do」部分,並添加您的url「listItems.xtml」,並使用我給出的方法,這是公共的無效methodInManagedBean()這是從 Swarne27

回答

9

您可以使用JSF preRenderView事件重定向到另一個頁面,按以下方式,

在你的index.xhtml文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 
<f:view> 
<ui:insert name="metadata"/> 
    <f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" /> 
<h:body></h:body> 
</f:view> 
</html> 

在受管理的豆中, 1的方式是

public class yourClass{ 

    FacesContext fc = FacesContext.getCurrentInstance(); 
    ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)fc.getApplication().getNavigationHandler(); 

    public void methodInManagedBean() throws IOException { 
     nav.performNavigation("list.do");//add your URL here, instead of list.do 
    } 
    } 

,或者您可以使用第二方式

public class yourClass{ 

    public void methodInManagedBean() throws IOException { 
     FacesContext.getCurrentInstance().getExternalContext().redirect("list.do");//add your URL here, instead of list.do 
    } 
    }