2012-12-25 53 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生成后被调用?