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
Variables jsf变量丢失值_Variables_Jsf_View Scope - Fatal编程技术网

Variables jsf变量丢失值

Variables jsf变量丢失值,variables,jsf,view-scope,Variables,Jsf,View Scope,我有一个名为bean1.java的托管Bean,它有一个名为found的布尔变量。变量表示是否找到了客户机 bean有一个方法validate(),该方法进入数据库并检查客户机是否存在,如果存在则将变量设置为found=true,如果不存在则将变量设置为“false” 然后我继续填写表格中的其他字段。现在,当我单击“保存”按钮时,它将显示方法saving()。如果varfound为true,则该方法必须执行一个操作,如果var为false,则必须执行另一个操作 但问题是,当我验证方法valida

我有一个名为
bean1.java的托管Bean,它有一个名为
found
的布尔变量。变量表示是否找到了客户机

bean有一个方法
validate()
,该方法进入数据库并检查客户机是否存在,如果存在则将变量设置为
found=true
,如果不存在则将变量设置为“false”

然后我继续填写表格中的其他字段。现在,当我单击“保存”按钮时,它将显示方法
saving()
。如果var
found
为true,则该方法必须执行一个操作,如果var
为false,则必须执行另一个操作

但问题是,当我验证方法
validate()
上设置为true的变量时,现在它的值为“false”

@ManagedBean
@查看范围//会话范围
公共类AgregarPoliza实现了可序列化{
找到的公共布尔值=false;
public void validate(){
//转到数据库并验证客户机是否存在(如果存在)
//将变量设置为真

found=true;//setFound(true);您应该像这样尝试在方法保存中调用validate方法

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(){
  this.validate();
//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
  }
public void validate()
{
//转到数据库并验证客户机是否存在(如果存在)
//将变量设置为真

found=true;//setFound(true);我已经使用了这个,现在工作正常,请检查它,现在它不能丢失值

AgregarPoliza.java

package com.best.uibeansTest;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean(name = "agregarPoliza")
@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 String saving(){
    validate();
//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
  }

return "test2.xhtml" ;
}
//get and set of the value found            
public boolean isFound() {
    return found;
}

public void setFound(boolean found) {
    this.found = found;
}

//get and set of the value found            


}
AgregarPoliza.java
包com.best.uibeansTest;
导入java.io.Serializable;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.SessionScoped;
@ManagedBean(name=“agregarPoliza”)
@会议范围
公共类AgregarPoliza实现了可序列化{
找到的公共布尔值=false;
public void validate(){
//转到数据库并验证客户机是否存在(如果存在)
//将变量设置为真

found=true;//setFound(true);请同时显示您的JSF页面,并更正bean java代码中的错误。再次出现
found=false
的唯一原因是在重新创建bean时。通过在bean的构造函数中添加一些日志来验证bean没有被销毁和重新创建。
AgregarPoliza.java

package com.best.uibeansTest;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean(name = "agregarPoliza")
@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 String saving(){
    validate();
//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
  }

return "test2.xhtml" ;
}
//get and set of the value found            
public boolean isFound() {
    return found;
}

public void setFound(boolean found) {
    this.found = found;
}

//get and set of the value found            


}
test2.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:f="http://java.sun.com/jsf/core"    
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:p="http://primefaces.org/ui"
   xmlns:ui="http://java.sun.com/jsf/facelets">
   <head>
      <title>JSF Tutorial!</title>
   </head>
   <h:body>
   <h2>Result</h2>
   <hr />
   <h:form>
    <p:commandButton value="Save" ajax="false" action="#{agregarPoliza.saving}"/>
      |
      found value:
      #{agregarPoliza.found}
   </h:form>

   </h:body>
</html>