jsf变量丢失值

时间:2014-01-15 23:42:42

标签: variables jsf view-scope

我有一个名为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            


    }

2 个答案:

答案 0 :(得分:0)

您应该尝试在方法中调用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
  }

答案 1 :(得分:0)

我hava使用这个,现在工作正常现在请检查它现在它不会失去价值...

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作为bean我正在显示下面的jsf代码,通过它检查这个..

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>  

单击保存按钮时找到的值更改为true。试试这个..