C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

Why JSF calls getters multiple times?

0 votes
200 views
asked Jan 21, 2014 by john Guru (27,140 points)

2 Answers

0 votes
This is caused by the nature of deferred expressions #{} (note that "legacy" standard expressions ${} behave exactly the same when Facelets is used instead of JSP). The deferred expression is not immediately evaluated, but created as a ValueExpression object and the getter method behind the expression is executed everytime when the code calls ValueExpression#getValue().

This will normally be invoked one or two times per JSF request-response cycle, depending on whether the component is an input or output component (learn it here). However, this count can get up (much) higher when used in iterating JSF components (such as <h:dataTable> and <ui:repeat>), or here and there in a boolean expression like the rendered attribute. JSF (specifically, EL) won't cache the evaluated result of the EL expression at all as it may return different values on each call (for example, when it's dependent on the currently iterated datatable row).

Evaluating an EL expression and invoking a getter method is a very cheap operation, so you should generally not worry about this at all. However, the story changes when you're performing expensive DB/business logic in the getter method for some reason. This would be re-executed everytime!

Getter methods in JSF backing beans should be designed that way that they solely return the already-prepared property and nothing more, exactly as per the Javabeans specification. They should not do any expensive DB/business logic at all. For that the bean's (post)constructor, initialization blocks and/or (action)listener methods should be used. They are executed only once at some point of request-based JSF lifecycle and that's exactly what you want.
answered Jan 21, 2014 by sara Guru (24,570 points)
0 votes
Let's say I specify an outputText component like this:

<h:outputText value="#{ManagedBean.someProperty}"/>

If  I print a log message when the getter for someProperty is called and load the page, it is trivial to notice that the getter is being called more than once per request (twice or three times is what happened in my case):

DEBUG 2014-01-18 02:31:40,104 (ManagedBean.java:13) - Getting some property

If the value of someProperty is expensive to calculate, this can potentially be a problem.

I googled a bit and figured this is a known issue. One workaround was to include a check and see if it had already been calculated:

private String someProperty;

public String getSomeProperty() {

    if (this.someProperty == null) {

        this.someProperty = this.calculatePropertyValue();

    }

    return this.someProperty;

}

The main problem with this is that you get loads of boilerplate code, not to mention private variables that you might not need.
answered Jan 21, 2014 by keem Expert (13,240 points)

Related questions

0 votes
2 answers 48 views
48 views asked Jan 21, 2014 by john Guru (27,140 points)
0 votes
2 answers 285 views
285 views asked Jan 23, 2014 by john Guru (27,140 points)
0 votes
1 answer 52 views
0 votes
1 answer 25 views
25 views asked Jan 3, 2014 by smith Geek (15,340 points)
0 votes
1 answer 415 views
415 views asked Jul 27, 2011 by Smita Advisor (7,140 points)
...