1

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.

Any help would be highly appreciated. Thanks!!

Tiny
  • 27,221
  • 105
  • 339
  • 599
jesantana
  • 1,241
  • 2
  • 14
  • 27
  • 1
    Maybe you've already answered your question and it's a bug. – zmirc Feb 11 '14 at 09:47
  • i have this issue with websphere. So i do a "workaround" to stop postconstruct fire every refresh... http://stackoverflow.com/a/21159145/3056912 : – xild Feb 11 '14 at 10:01
  • 2.0.2 is an early stage of the 2.x implementation. 2.0.20 and 2.1.14 are already available, go with one of them. They should work without making changes into your code. – Aritz Feb 11 '14 at 13:44

1 Answers1

0

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.

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.

jesantana
  • 1,241
  • 2
  • 14
  • 27
  • 1
    The instance of the object _is_ changing. The identity hashCode would stay the same. Your view bean is likely being serialized by the [StateManager](http://docs.oracle.com/javaee/7/api/javax/faces/application/StateManager.html) between requests. – McDowell Feb 18 '14 at 16:45