0

PhaseListener调用initialize方法中。

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,上面的代码将打印myBooleantrue而不是false

myBean是在request范围内。因为,这是一个新的要求,我不得不相信myBoolean它是新分配的true价值。

我该如何克服呢?我的意思是,当secondMethod被调用时,如果myBooleanfalse,那么它也应该falsesecondMethod。为什么myBoolean总是这样true

4

2 回答 2

0

你确定你的初始化方法被调用了吗?将@PostConstruct注解放在初始化方法中,以确保在生成bean 后调用它怎么样?

于 2012-12-26T10:33:06.710 回答
0

我解决了我的问题。我的问题有两个部分。

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.我问题的答案。

于 2012-12-26T13:41:04.737 回答