Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 更改请求范围bean中的初始值_Jsf_Jsf 1.2 - Fatal编程技术网

Jsf 更改请求范围bean中的初始值

Jsf 更改请求范围bean中的初始值,jsf,jsf-1.2,Jsf,Jsf 1.2,在PhaseListeneram中调用initialize方法 public class myBean implements Serializable { private boolean myBoolean = "true"; public void initialize() { if(someCondition) { this.setMyBoolean(true); } else {

PhaseListener
am中调用
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;  
}

公共字符串secondMethod()
{
debug(“myBoolean变量的值为:”+this.isMyBoolean());
返回null;
}
当用户单击
someLink
时,上述代码将
myBoolean
打印为
true
,而不是
false

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


我怎样才能克服这个问题?我的意思是,当调用
secondMethod
时,如果
myBoolean
false
,那么在
secondMethod
中也应该是
false
。为什么
myBoolean
始终保持
true

您确定正在调用初始化方法吗?在初始化方法中添加@PostConstruct注释,以确保在生成bean后调用它,怎么样?

我解决了我的问题。我的问题分为两部分

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;  
}

//使用f:param发送实际值
公共字符串secondMethod()
{
字符串newValueIs=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(“newValue”);
log.debug(“myBoolean变量的值为:”+newValueIs);//如果为false,则打印false;如果为true,则打印true
返回null;
}

然而,我仍然没有得到关于
2的答案。
我的问题的要点。

也许回发的某些条件是正确的?不,如果它是
false
它将始终是
false
,如果它是
true
它将始终是
true
我认为你应该简单地进行一些调试。在initialize方法中设置一个断点,并检查是否为回发调用该断点,以及是否更改了布尔值。如果没有其他地方可以更改布尔值,则必须通过initialize方法调用它。我进行了必要的调试<代码>初始化()方法没有为回发调用,也没有更改布尔变量的值。当我点击
someLink
时,它唯一的调用是
secondMethod()
,它不是布尔变量的where设置值。@chkal我在下面给出了答案。也许你能回答第二点