使用JSTL /核心标签搞砸了ViewScoped Bean

时间:2011-05-18 01:54:30

标签: jsf jsf-2 jstl mojarra

我想我在Mojarra 2.1.0中遇到了一个错误。也许我错过了一些东西,但如果我能看到它就该死。

我依赖很多@ViewScoped bean来保存状态,而浏览器会向服务器执行大量的AJAX。我发现当我使用某些标签时,@ ViewScoped bean开始重新实例化它不应该。这是我的测试用例支持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 });
}

}

这是使用它的JSF Facelets页面:

<!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”命令按钮时,每次都会创建一个新的辅助bean实例,就像它是一个@RequestScoped bean一样。我可以通过构造函数中的log()调用来看到这一点。

如果将bean更改为@SessionScoped,则不会发生这种情况。无论单击按钮多少次,都会得到一个bean实例。

但是 - 如果你将它保留为@ViewScoped,并且你取出 c:foreach 元素及其内容,它现在不再重新实例化每次点击的bean。换句话说,它现在按预期工作。

这是一个mojarra bug还是我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

这是一个已知的“错误”:issue 1665。这是关于部分国家储蓄的鸡蛋问题。

但是,在您的情况下,您也可以使用<ui:repeat>

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

最好的办法是在使用@ViewScoped时尽量避免使用JSTL标记。唯一的选择是通过web.xml中的上下文参数禁用部分状态保存:

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

但这会使观点更多地占用内存。