简体   繁体   English

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

[英]JSF-ViewScope bean reinstantiated in every request

I have seen this bug described in many places but always the causes are different. 我已经在很多地方看到过描述此错误的原因,但总是有不同的原因。 Post like this one states that the problem of re-instantiation only occurs, when you include a tag handler library on your page. 这样的帖子指出,只有在页面上包含标记处理程序库时,才会发生重新实例化的问题。 However, I have an empty project with a page like the following 但是,我有一个空项目,页面如下所示

<?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>

With the backing bean like this 像这样的豆

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

And the following dependences 还有以下依赖

<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>

and every time I click on the button a different object is created. 每次我单击按钮时,都会创建一个不同的对象。 Apparently this page is as simple as possible and I have not triggered any of the possible causes of the bugs, so It always happen or am I missing something? 显然,此页面尽可能简单,并且我还没有触发任何可能的错误原因,所以它总是会发生或者我错过了什么吗?

I tested changing the dependencies to 2.2.0 and it works as expected but unfortunately due to project restrictions, I need to keep the version 2.0.2 of JSF. 我测试了将依赖关系更改为2.2.0,它可以按预期工作,但是不幸的是,由于项目限制,我需要保留JSF的2.0.2版本。

Any help would be highly appreciated. 任何帮助将不胜感激。 Thanks!! 谢谢!!

Actually, I have found that the instance remain the same. 实际上,我发现实例保持不变。 The problems was that when clicking the button I always saw different hashCodes printed in the toString of the method like this. 问题是,当单击按钮时,我总是看到这样的方法的toString中印有不同的hashCode。

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

and this led me to think there were different instances. 这使我认为存在不同的情况。

Although is the same instance, i believe this behaviour is incorrect because the hashCode of an object is not supposed to change during its lifetime. 虽然是同一实例,但我认为这种行为是错误的,因为对象的hashCode不应在其生命周期内发生变化。

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

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