【发布时间】:2023-03-21 23:11:01
【问题描述】:
我有一个名为 bean1.java 的托管 Bean,它有一个名为 found 的布尔变量。该变量表示是否找到了客户。
bean 有一个方法validate(),该方法进入数据库并检查客户端是否存在,如果存在则将变量设置为found=true,如果不存在则设置为'false'。
然后我继续填写表单中的其他字段。现在,当我单击按钮保存时,它是方法saving()。如果 var found 为真,则该方法必须执行一个操作,如果为假,则必须执行另一个操作。
但问题是当我验证在方法validate() 上设置为true 的变量时,现在它的值是“false”。
@ManagedBean
@ViewScoped //SessionScoped
public class AgregarPoliza implements Serializable{
public boolean found=false;
public void validate(){
// go to data base and validate if the client exist, when exist
// set variable true
found = true; // setFound(true); <--- i already try this way too
}
public void saving(){
//it has two actions to do but need to know the value of found
System.out.println("my var found has" + found); //this system.out shows false
if(found ==true){
//makes an action
}
else{
//makes another action
}
}
//get and set of the value found
}
【问题讨论】:
-
请同时显示您的 JSF 页面并更正您的 bean java 代码中的拼写错误。
-
found=false再次出现的唯一原因是重新创建 bean 时。通过在 bean 的构造函数中添加一些日志记录来验证 bean 没有被销毁和重新创建。
标签: variables jsf view-scope