4

インデックス ページ (私の場合は index.xhtml) を用意することをお勧めします。インデックスページでいくつかのアクションを渡したいです(たとえば、strutsで:<c:redirect url="list.do" />リンクやボタンなしでstrutsアクションクラスに移動します)ナビゲーションを使用したい場合は、commandLink-sまたはボタンを使用する必要があります)。onclick JavaScript関数で書くことはできます<h:commandButton>が、これが最善の選択肢だとは思いません。

私は (JSF 2.0 を使用して) JSF にまったく慣れていないので、アドバイスが必要です。インデックス ページからコントローラーのアクションにリダイレクトするためのベスト プラクティスは何ですか?

///新しいバージョン

<!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);
    }
}
4

1 に答える 1

11

JSFpreRenderViewイベントを使用して、次の方法で別のページにリダイレクトできます。

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>

マネージドBeanでは、 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
    }
    }

または、 2番目の方法を使用できます

    public class yourClass{ 

    public void methodInManagedBean() throws IOException {
         FacesContext.getCurrentInstance().getExternalContext().redirect("list.do");//add your URL here, instead of list.do
    }
    }
于 2012-10-10T12:05:16.180 に答える