每次请求都会重新创建ViewScoped托管bean

时间:2015-05-01 15:24:16

标签: jsf jsf-2 view-scope

我设法获得了一个简单的ViewScoped页面。我不知道我是不是不明白或者这个页面不能正常工作。 每个请求都会创建一个新的Test对象,即使它是ViewScoped,我也永远无法收回我设置的theString变量。

带有输入字段的普通模板,用于设置值的按钮和用于刷新显示值的按钮:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

    <f:view contentType="text/html">
        <h:head>
        </h:head>

        <h:body>
            <h:form>
                <p:commandButton process="theInput" value="Set value" />
                <p:inputText id="theInput" value="#{test.theString}" /> --
            </h:form>

            <h:form>
                <p:commandButton process="theArea" update="theArea" value="Display value" />
            </h:form>

            <h:panelGroup id="theArea">
                #{test.theString}
            </h:panelGroup>
        </h:body>
    </f:view>
</html>

豆:

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;

@ManagedBean(name = "test")
@ViewScoped
public class Test implements Serializable {

    private String theString = "initial";

    public String getTheString() {
        return theString;
    }

    public void setTheString(String theString) {
        this.theString = theString;
    }
}

我使用的是Mojarra 2.2.7,Primefaces 5.2和Glassfish 4.1

Test对象不应该保持其状态吗?我在这里找不到什么东西?

0 个答案:

没有答案