jsf 2 - JSF ViewScope beans incorrect hashcode implementation? -


I am developing a simple application on JSF with the following dependencies

  & lt; Dependency & gt; & Lt; Group & gt; Org.apache.myfaces.core & lt; / Group & gt; & Lt; ArtifactId & gt; Myfaces-implant & lt; / ArtifactId> & Lt; Version & gt; 2.0.2 & lt; / Edition & gt; & Lt; / Dependencies & gt; & Lt; Dependency & gt; & Lt; Group & gt; Org.apache.myfaces.core & lt; / Group & gt; & Lt; ArtifactId & gt; Myfaces-api & lt; / ArtifactId> & Lt; Version & gt; 2.0.2 & lt; / Edition & gt; & Lt; / Dependencies & gt;  

And a simple page with bean.

XHTML

  & lt ;? Xml version = "1.0" encoding = "UTF-8" & gt; & Lt ;; DOCTYPE HTML PUBLIC "- // W3C // DTT XHTML 1.0 Transcription // N" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> & Lt; Html xmlns = "http://www.w3.org/1999/xhtml" xmlns: h = "http://java.sun.com/jsf/html" xmlns: f = "http: // java sun.com / Jsf / core "& gt; & Lt; Top & gt; & Lt; Title & gt; & Lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; H: form & gt; & Lt; F: view & gt; & Lt; H: commandbutton id = "octxempaneltext bt" value = "click" action = "# {other controller. Sleep some}" /> & Lt; / Ch: view & gt; & Lt; / H: form & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

Java code

  import java.io.Serializable; Import javax.faces.bean.ManagedBean; Import javax.faces.bean.ViewScoped; @ Management (name = "other controller") @ViewScoped public square Other Siriyljhebl {private static final long serial VERSIONUID = 6493758917750576706L; Make a public string something () {System.out.println (this); Return ""; }}  

If I click on the command button several times according to the string source code then

  class name + hashcode  

but it writes something like the following

  de.controller.Other@118ea91 de.controller.Other@1e8f930 de.controller.Other@1a38f3c  

Thus, every time we click, see There is a different hashode for the seed beans. Is this wrong? I have tried the season with beans and it has not happened.

Edit

I modified the following suggestions in the following way.

  import java.io.Serializable; Import javax.annotation.PostConstruct; Import javax.faces.bean.ManagedBean; Import javax.faces.bean.ViewScoped; @ Management (name = "other controller") @ViewScoped public square Other Siriyljhebl {private static final long serial VERSIONUID = 6493758917750576706L; @PostConstruct Public Zero init () {System.out.println ("Post Creation"); } Public other () {System.out.println ("Constructor"); } Public Zero doSome () {System.out.println (this); }}  

And I got this output after clicking several times.

  constructor post construction de.controller.Other@b748dd de.controller.Other@1620dca De.controller.Other@116358 de.controller.Other@73cb2d de.controller.Other@1cc3ba0  

Therefore, the Constructor and Post Constructor Methods apply only once, and in spite of this, the object still has a different hash code in each call. This is strange because the constructor with no parameter is called on a managed bitterness, but it is not so, so I can not even understand how the object can be created several times?

When you return non- null to an action method A new scene is created.

If you want to keep the current view alive, simply return null or zero to the action method.

Example

  Public Zero doSome () {System.out.println (this); }  

See also:


He said that it is a conclusion the problem is correct, but the basis on which you based on your findings, "wrong hash code implementation" , there is no absolute sense they just physically separate beans for example, a default class Add constructor, put a breakpoint on it and you will see that every time you return from your action method, by creating a new instance

Comments