1

Mojarra2.1.0でバグが発生したと思います。多分私は何かを逃したが、それを見ることができれば気にしない。

ブラウザがサーバーに対して多くのAJAXを実行している間、状態を保存するために多くの@ViewScopedBeanに依存しています。特定のタグを使用すると、@ViewScopedBeanが本来あるべきではないときに再インスタンス化され始めます。これが私のテストケースバッキングBeanです。

/*
 * TestStuff.java
 */
package test;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

/**
 * Backing bean for test.xhtml -- working out AJAX/SVG connection
 *
 */

@ManagedBean 
@ViewScoped
public class TestStuff implements Serializable {

private int counter = 0;

public TestStuff() {
    log("TestStuff(): {0}", this);
}

public String getRandomNumber() { 
    int i = (int) (Math.random() * 1000000.0);
    return String.format("%d", i);
}

public int getCounter() { return counter; }

public List<String> getStuff() {
    return Arrays.asList("big", "bad", "wolf");
}

public void pushButton(ActionEvent evt) {
    log("TestStuff.pushButton({0}): {1}", 
            new Object[] { evt, ++counter });
}

}

そして、これを使用するJSFFaceletsページは次のとおりです。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.prime.com.tr/ui"
  xmlns:ui="http://java.sun.com/jsf/facelets"  
  xmlns:f="http://java.sun.com/jsf/core" 
  xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
    <title>Test Page</title>
</h:head>
<h:body>
    <h1>Test Page</h1>
    <p>If you are reading this text, the server
        is not properly configured.</p>
    <ui:composition id="compRoot" template="/template5.xhtml">

        <ui:define name="content">
            <h1>Test</h1>
            <h:form id="formTest">

                <p:commandButton value="Do Me" 
                                 actionListener="#{testStuff.pushButton}"
                                 update="testUpdate" />

                <p:panel id="testUpdate" >
                    <h:outputText value="Random output is: " />
                    <h:outputText
                        value="#{testStuff.randomNumber}"
                        />
                    <h:outputText value=" Counter is: "/>
                    <h:outputText 
                        value="#{testStuff.counter}"
                        />
                </p:panel>

                <h:panelGrid columns="5" border="1" >
                    <c:forEach items="#{testStuff.stuff}" var="x">
                        <h:outputText value="#{x}" />
                    </c:forEach>
                </h:panelGrid>                  

            </h:form>
        </ui:define>
    </ui:composition>
</h:body>
</html>

だからここに何が悪いのかがあります。[Do Me]コマンドボタンをクリックすると、@ RequestScoped Beanであるかのように、バッキングBeanの新しいインスタンスが毎回作成されます。これは、コンストラクターのlog()呼び出しで確認できます。

Beanを@SessionScopedに変更した場合、これは発生しません。ボタンが何度クリックされても、Beanのインスタンスを1つ取得します。

ただし、@ ViewScopedのままにして、c:foreach要素とそのコンテンツを削除すると、クリックするたびにBeanが再インスタンス化されなくなります。つまり、期待どおりに機能するようになりました。

これはクロサギのバグですか、それとも私はここで何か間違ったことをしていますか?

4

1 に答える 1

2

これは既知の「バグ」です:問題1665。これは、部分的な状態の保存に関する鶏卵の問題です。

ただし、あなたの場合は、を使用することもできます<ui:repeat>

<ui:repeat value="#{testStuff.stuff}" var="x">
    <h:outputText value="#{x}" />
</ui:repeat>

最善の策は、を使用するときにJSTLタグを回避すること@ViewScopedです。唯一の代替手段は、次のコンテキストパラメータによる部分的な状態保存を無効にすることですweb.xml

<context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
</context-param>

しかし、それはビューをより多くのメモリを占有させます。

于 2011-05-18T02:19:00.197 に答える