0

JSF 2.1 の GET 要求で preRenderView イベント リスナーを動作させることができません。

私はそれについて多くのことを見つけましたが、何もうまくいかないようです
http://developer.am/j2eetutorial/jsf/?page=jsf-2-prerenderviewevent-example JSF、Spring、PreRenderViewEvent http://balusc.blogspot.dk/2011/09/communication-in-jsf-20 .html#ProcessingGETRequestParameters



4 つの挿入ブロックを含むテンプレートがあり、それらすべての場所にイベント コードを挿入しようとしましたが、うまくいきませんでした。f:metadata タグの有無にかかわらず試してみました。

<f:event type="preRenderView" listener="#{applicationData.redirectIfNoResults}" />

豆:

@ManagedBean
@ApplicationScoped
public class ApplicationData implements Serializable {
    public void redirectIfNoResults() throws IOException { 
        if (getTotal() < 1) { 
            ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); 
            ec.redirect(ec.getRequestContextPath() + "/noResults.xhtml"); 
        } 
    } 
    ...
}

テンプレート:

<?xml version='1.0' encoding='UTF-8' ?>
<!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">
    <ui:insert name="beforeHeader" />
    <f:view>
        <ui:insert name="inView" />
    </f:view>
    <h:head>
        <meta http-equiv="cache-control" content="no-store" />
        <link href="style.css" rel="stylesheet" type="text/css" />
        <title>Quick Poll</title>
        <ui:insert name="header" />
    </h:head>
    <h:body>
        <h1>Quick Poll</h1>
        <ui:insert name="content" />
    </h:body>
</html>

意見:

    <ui:define name="content">
        #{applicationData.question}?<p/>
        <h:panelGrid columns="3" border="0">
                Yes: 
                <h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.yes/applicationData.total}"/>
                #{applicationData.yes}
                <h:outputText value="No:"/> 
                <h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.no/applicationData.total}"/>
                #{applicationData.no}
        </h:panelGrid>
    </ui:define>
</ui:composition>

それを機能させる方法を理解するのを手伝ってください..

更新 1:
BalusC の提案に従って変更を加えましたが、まだ機能していません..

テンプレート:

<?xml version='1.0' encoding='UTF-8' ?>
<!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">
    <h:head>
        <meta http-equiv="cache-control" content="no-store" />
        <link href="style.css" rel="stylesheet" type="text/css" />
        <title>Quick Poll</title>
        <ui:insert name="header" />
    </h:head>
    <h:body>
        <h1>Quick Poll</h1>
        <ui:insert name="content" />
    </h:body>
</html>

意見:

<?xml version='1.0' encoding='UTF-8' ?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:f="http://java.sun.com/jsf/core"
     template="template.xhtml">
    <ui:define name="content">
        <f:event listener="#{applicationData.redirectIfNoResults}" type="preRenderView"></f:event> 
        #{applicationData.question}?<p/>
        <h:panelGrid columns="3" border="0">
                Yes: 
                <h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.yes/applicationData.total}"/>
                #{applicationData.yes}
                <h:outputText value="No:"/> 
                <h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.no/applicationData.total}"/>
                #{applicationData.no}
        </h:panelGrid>
    </ui:define>
</ui:composition>
4

3 に答える 3

2

これ<f:view>は正しく使用されていません。ビュー全体をラップする必要があります。それを削除する (JSF が暗黙的に作成する) か、少なくとも<h:head><h:body>タグを含むビュー全体をラップします。

ちなみに、 は<f:event>に入る必要はありません<f:metadata>。にのみ適用され<f:viewParam>ます。の<f:event>結果に依存するリスナーは、同じブロックに配置されている場合<f:viewParam>ある<f:metadata>が、それ自体は要件ではありません<f:event>

あなたの場合、それを入れるだけの方が簡単です<ui:define name="content">

于 2012-09-09T12:08:47.160 に答える
0

ComponentSystemEventがメソッドシグネチャにないようです。

メソッドは次のようになります。

public void newRequest(final ComponentSystemEvent event) {
System.out.println("Someone requested me");
}

次に、発信者をテンプレートまたは個別のビューに配置しますが、違いはありません。

<f:event listener="#{userSessionAction.newRequest}" type="preRenderView"></f:event>
于 2012-09-09T11:56:35.220 に答える
0

ビュー スコープ Bean で PostConstruct メソッドを使用してソリューションを作成することになりました。
このように: JSF 2.0 を使用したページ読み込み時にパラメーターを使用してバッキング Bean を初期化する

豆:

@ManagedBean 
@ViewScoped
public class ResultsController {
    @ManagedProperty(value="#{applicationData.total}")  
    private int total; 
    @ManagedProperty(value="#{applicationData.yes}")  
    private int yes; 
    @ManagedProperty(value="#{applicationData.no}")  
    private int no; 

    @PostConstruct 
    public void postConstruct() {
        if (getTotal() < 1) { 
            ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); 
            try {
                ec.redirect(ec.getRequestContextPath() + "/noResults.jsf");
            } catch (IOException e) {
                System.out.println("noResults.jsf redirect failed.");
                e.printStackTrace();
            } 
        } 
    }
    ...
}

意見:

<?xml version='1.0' encoding='UTF-8' ?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:f="http://java.sun.com/jsf/core"
     template="template.xhtml">
    <ui:define name="content">
        #{applicationData.question}?<p/>
        <h:panelGrid columns="3" border="0">
                Yes: 
                <h:panelGrid bgcolor="black" height="20" width="#{300*resultsController.yes/resultsController.total}"/>
                #{resultsController.yes}
                <h:outputText value="No:"/> 
                <h:panelGrid bgcolor="black" height="20" width="#{300*resultsController.no/resultsController.total}"/>
                #{resultsController.no}
        </h:panelGrid>
    </ui:define>
</ui:composition>
于 2012-09-10T13:42:59.637 に答える