繁体   English   中英

在每个请求中都重新实例化了JSF-ViewScope bean

[英]JSF-ViewScope bean reinstantiated in every request

我已经在很多地方看到过描述此错误的原因,但总是有不同的原因。 这样的帖子指出,只有在页面上包含标记处理程序库时,才会发生重新实例化的问题。 但是,我有一个空项目,页面如下所示

<?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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <head>
        <title></title>
    </head>
    <body >

    <h:form>
        <f:view >
            <h:commandButton id="otxMainPanelTextBt" value="click" 
                             action="#{otherController.doSome}"/>
        </f:view>
    </h:form>

</body>
</html>

像这样的豆

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


@ManagedBean(name = "otherController")
@ViewScoped
public class Other implements Serializable {

    private static final long serialVersionUID = -6493758917750576706L;

    public String doSome() {
        System.out.println(this);
        return "";
    }
}

还有以下依赖

<dependency>
  <groupId>org.apache.myfaces.core</groupId>
  <artifactId>myfaces-impl</artifactId>
  <version>2.0.2</version>
</dependency>
<dependency>
  <groupId>org.apache.myfaces.core</groupId>
  <artifactId>myfaces-api</artifactId>
  <version>2.0.2</version>
</dependency>

每次我单击按钮时,都会创建一个不同的对象。 显然,此页面尽可能简单,并且我还没有触发任何可能的错误原因,所以它总是会发生或者我错过了什么吗?

我测试了将依赖关系更改为2.2.0,它可以按预期工作,但是不幸的是,由于项目限制,我需要保留JSF的2.0.2版本。

任何帮助将不胜感激。 谢谢!!

实际上,我发现实例保持不变。 问题是,当单击按钮时,我总是看到这样的方法的toString中印有不同的hashCode。

constructing the instance
de.controller.Other@118ea91
de.controller.Other@1e8f930
de.controller.Other@1a38f3c

这使我认为存在不同的情况。

虽然是同一实例,但我认为这种行为是错误的,因为对象的hashCode不应在其生命周期内发生变化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM