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我在下面给出了答案。也许你能回答第二点