2014-01-15 1 views
1

found이라는 부울 var가있는 bean1.java이라는 관리 Bean이 있습니다. 변수는 클라이언트를 찾았는지 여부를 나타냅니다.jsf 변수 손실 값

빈에는 DB로 이동하여 클라이언트가 있는지 확인하고 변수가 존재하는 경우 found=true으로 설정하고 그렇지 않은 경우에는 'false'로 설정합니다.

그런 다음 양식에서 다른 입력란을 계속 작성합니다. 이제 버튼 저장을 클릭하면 방법이 saving()이됩니다. 이 메서드는 var found이 true이면 작업을 수행하고 false이면 다른 작업을 수행해야합니다.

그러나 문제는 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    


    } 
+1

JSF 페이지도 표시하고 bean java 코드의 오타를 수정하십시오. – elbuild

+0

'found = false'가 다시 bean이 다시 작성되는 유일한 이유입니다. 빈에 대한 생성자에 일부 로깅을 추가하여 빈이 삭제 및 다시 작성되지 않는지 확인하십시오. – kolossus

답변

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 
    } 
0

여기

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    


} 

난 그냥 내가 통해 아래 JSF 코드를 콩으로 agregarPoliza를 얻을 보여주는입니다 ... 제발 지금은 손실되지 수 값을 확인 내가 하바 이것을 사용이 지금은 잘 작동 이걸 확인해 봐.

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로 변경됩니다. 이것을 시도해보십시오.