2012-12-25 49 views
0

PhaseListener上午調用initialize方法。更改Request scoped bean中的初始值

public class myBean implements Serializable 
{ 
private boolean myBoolean = "true"; 

public void initialize() 
    { 
    if(someCondition) 
     { 
      this.setMyBoolean(true); 
     } 
    else 
     { 
      this.setMyBoolean(false); // Lets assume myBoolean gets set to false here 
     } 
    } 
} 

該方法執行後,index.jsf呈現給用戶。

index.xhtml頁,下面的代碼是有..

<h:commandLink action="#{myBean.secondMethod}" value="someLink"> 
</h:commandLink> 

public String secondMethod() 
{ 
    log.debug("Value of myBoolean variable is: " +this.isMyBoolean()); 
    return null; 
} 

當用戶點擊someLink,上述代碼將打印myBoolean作爲true代替false

myBeanrequest範圍內。因爲,它的一個新的請求,我們不得不相信myBoolean是新分配的true值。

我如何克服這個問題?我的意思是,當secondMethod被調用時,如果myBooleanfalse,那麼它也應該是false,secondMethod。爲什麼myBoolean始終保持爲true

+0

也許someCondition是真的回發? – chkal

+0

沒有如果'FALSE'它總是會返回FALSE,如果它是TRUE;它永遠是TRUE; –

+0

我想你應該簡單地做一些調試。在initialize方法中設置一個斷點並檢查它是否被調用回發,以及它是否更改布爾值。如果沒有其他地方你改變布爾值,它必須被初始化方法調用。 – chkal

回答

0

我解決我的問題。在我的問題中有2個部分。

1.我該如何克服這個問題?

2.爲什麼myBoolean始終保持爲真?下面

答案是點1.

<h:commandLink action="#{myBean.secondMethod}" value="someLink"> 
    <f:param name="newValue" value="#{myBean.myBoolean}"></f:param> // Use f:param to send the actual value 
</h:commandLink> 


public String secondMethod() 
{ 
    String newValueIs = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("newValue"); 
    log.debug("Value of myBoolean variable is: " +newValueIs); //Prints false if it was false and true if it was true 
    return null; 
} 

但是,我仍然不是我的問題2.點得到答案。

0

你確定你的初始化方法被調用?怎麼樣把一個 @PostConstruct註釋到初始化方法,以確保它會在bean生成後被調用?