2

I have a managed Bean called bean1.java that has a boolean var called found. The variable says if a client has been found or not.

The bean has a method validate() that goes to the DB and checks if the client exists, and sets the variable to found=true if it exists or 'false' if it doesn't.

Then I keep completing other fields in my form. Now, when I click the button save, it the method saving(). That method has to do an action if the var found is true and another action if it's false.

But the problem is when I validate the variable that has been set to true on the method validate(), now it has the value "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            


    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Please show your JSF page as well and correct the typos in your bean java code. – elbuild Jan 15 '14 at 23:45
  • The only reason why `found=false` again is when the bean is being recreated. Verify that the bean is not being destroyed and recreated by adding some logging in a constructor for the bean. – kolossus Jan 16 '14 at 14:51

2 Answers2

0

You should try calling the validate method in the method saving like this.

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
  }
Apurv
  • 3,723
  • 3
  • 30
  • 51
MrRavi
  • 35
  • 7
0

i hava use this and this is working fine now check it please now it can not lost value...

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            


}

here i just get agregarPoliza as bean i am showing jsf code below through which i check this..

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>  

on clicking of save button found value is changed to true. try this..

islamuddin
  • 185
  • 5
  • 17