简体   繁体   中英

Multiple log statements from single println

I am writing the size of my arraylist just for debugging purposes but I notices that I get the value multiple times in the log window in Eclipse. It is different for each time, and it applies other places where I debug with System.out.println(..) Any reason why this happens?


Log window

INFO: 4
INFO: 4
INFO: 4
INFO: 4

View

<f:metadata>
    <f:event listener="#{defaultNewQuestionHandler.init}" type="preRenderView" />
</f:metadata>

Backingbean

import java.io.Serializable;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ViewScoped
@ManagedBean
public class DefaultNewQuestionHandler extends QuestionHandler implements Serializable {

    private static final long serialVersionUID = -6986655384538762284L;

    @Override
    public void init() {
        answers = new ArrayList<Answer>();

        for (int i = 0; i < 4; i++) {
            addAnswerAlternative();
        }

        System.out.println(answers.size());
    }
}

You get the value multiple times in the log due to JSF's Request Respond Lifecycle. The initial request results in 4 log entries because JSF executes the Restore View, Apply Request Values, Process Events and Render Response phase.

JSF tends to call methods on the backing bean more than once (as mentioned eg in this Seam performance tips article). It's generally a good idea to calculate the result, stick it in a field, and have the JSF page reference a method that just returns the field value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM