문제

I am developing an application using java and PrimeFaces. Below is the java code i have written:

class ShowRecordsManagedBean{
private List<RecordDTO> recordInfoList = null;
...
..
    public List<RecordDTO> getRecordList() {
        boolean flag=true;
        try {
            if(dataList==null){
                //logic here
                flag=false;
            }
            if(dataList!=null && flag){
                //Logic here
            } 
        } catch (Exception e) {

        }
        return dataList;
    }

Issue is when page loads each time the control has to go the below if condition:

   if(dataList==null){
        //logic here
        flag=false;
    }

But only for the time time, its going to the above mentioned if(..) condition, later even if i refresh the page its going to other if(..) present.

if(dataList!=null && flag){
            //Logic here
        } 

How to modify the condition , that each time when i refresh the page it has to go to first if(..) condition, basically the object values are not becoming null even when page loads.I initialized all object to null initally while declaring. I need to close the browser or delete the cache to make control enter into other if(dataList!=null && flag) condition.

Modified as below:

@ManagedBean
@ViewScoped
public class ShowRecordsManagedBean {
    //fields declaration
private List<RecordsDTO> recordInfoList = null;
    @PostConstruct
    public void init() {
        recordInfoList = null;
    }
    //getters, setters and other methods...
public List<RecordDTO> getRecordList(){
..
}
}

Still the problem not yet solved. When i refresh the page dataList should become null, but its holding previous value. Please suggest. Thanks.

도움이 되었습니까?

해결책

The problem is that your bean is created as @RequestScoped, so it will be recreated on every request. In order to make it live longer you should wide the scope at least to @ViewScoped. In this way, the bean will live while the client is in the same view, it is very useful when handling ajax requests in the same view.

Also, you should remove any business logic from the getters/setters of your fields since the getters may be called several times by the view (JSP or Facelets). You should load any initial data for your bean in a @PostConstruct annotated method.

Your bean should look like this:

@ManagedBean
@ViewScoped
public class ShowRecords {
    //fields declaration
    @PostConstruct
    public void init() {
        //initialize your fields here
        //logifc to initialize dataList goes here
    }
    //getters, setters and other methods...
}

More info:


Not part of the answer, but since you're using JSF 2 and PrimeFaces 4, you should stop using JSP as your view technology and move to Facelets.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top