Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

jsf - Use of JSTL/core tags screws up ViewScoped Bean

I think I have run into a bug in Mojarra 2.1.0. Maybe I missed something but damned if I can see it.

I rely a lot of @ViewScoped beans to save state whilst the browser does a lot of AJAX to the server. I find when I use certain tags, the @ViewScoped bean starts getting re-instantiated when it shouldn't be. Here is my test case backing 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

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


And here is the JSF Facelets page that uses it:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    <title>Test Page</title>
    <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">
            <h:form id="formTest">

                <p:commandButton value="Do Me" 
                                 update="testUpdate" />

                <p:panel id="testUpdate" >
                    <h:outputText value="Random output is: " />
                    <h:outputText value=" Counter is: "/>

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


So here is what goes wrong. When you click on the "Do Me" command button, a new instance of the backing bean gets created each time, just as if it were a @RequestScoped bean. I can see this via the log() call in the constructor.

If you change the bean to @SessionScoped, this doesn't happen. You get one instance of the bean no matter how many times the button is clicked.

HOWEVER -- if you leave it as @ViewScoped, and you take out the c:foreach element and its content, it now no longer re-instantiates the bean each click. In other words it now works as expected.

Is this a mojarra bug or am I doing something wrong here?

See Question&Answers more detail:os

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a known "bug": issue 1665. It's a chicken-egg issue with regard to partial state saving.

In your case, however, you could also just use <ui:repeat>.

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

Your best bet is to try to avoid JSTL tags when using @ViewScoped. The only alternative is to disable partial state saving by a context param in web.xml:


But it makes the views more memory hogging.

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share