【问题标题】:jsf variable lost value [duplicate]jsf变量丢失值[重复]
【发布时间】: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


【解决方案1】:

你应该尝试像这样在方法保存中调用 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
  }

【讨论】:

    【解决方案2】:

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

    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。试试这个。。

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 1970-01-01
      • 2021-04-28
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多