2014-02-11 51 views
1

我曾經見過很多地方描述過的這個bug,但是總是會有不同的原因。像this之類的文章指出,只有在頁面中包含標籤處理程序庫時,纔會出現重新實例化的問題。但是,我有一個類似如下JSF-ViewScope bean在每個請求中都重新生成了

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

隨着支持bean這樣

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版本。

任何幫助將不勝感激。 謝謝!

+1

也許你已經回答了你的問題,這是一個錯誤。 – zmirc

+0

我有這個問題與websphere。 因此,我做了一個「解決方法」,以停止postconstruct每刷新火災...... http://stackoverflow.com/a/21159145/3056912 : – xild

+0

2.0.2是2.x實施的早期階段。 2.0.20和2.1.14已經可用,請與其中一個。他們應該在不更改代碼的情況下工作。 –

回答

0

其實,我發現實例保持不變。問題是,當點擊按鈕時,我總是看到不同的hashCode打印在方法的toString中,就像這樣。

constructing the instance 
[email protected] 
[email protected] 
[email protected] 

這讓我覺得有不同的例子。

雖然是同一個實例,但我相信這種行爲是不正確的,因爲對象的hashCode在其生命週期中不應該改變。

+1

對象_is_變化的實例。身份hashCode將保持不變。您的視圖bean可能會在請求之間由[StateManager](http://docs.oracle.com/javaee/7/api/javax/faces/application/StateManager.html)序列化。 – McDowell

相關問題