2010-11-22

Getters called multiple times in JSF

In JSF, getters of your beans might be called multiple times per request - it’s a fact. Therefore making a data source call from the getter will have a negative impact on your performance.

Suppose you have a page with a form on it. The user fills and submits the form. There are two scenarios possible - submit success or failure. The expensive call could be acquiring a list of options to choose from on the form.

  1. The user opens the page.
  2. Make just one expensive database call. I.e. don’t make the same call twice.
  3. The user fills and submits the form.
  4. Display the success page, do not make the expensive database call.

  1. The user opens the page.
  2. Make just one expensive database call. I.e. don’t make the same call twice.
  3. The user fills and submits the form.
  4. Validation error occurs. Return to the form page and make the expensive call (to reload the list of options, for instance).

For these two scenarios putting the expensive database call in the constructor or getter will not work. You will either get too many calls or too few.

Solution


And here is the solution.

package me.m1key.sample.singlecall;

import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
// ...

@Named
@RequestScoped
public class UserBean {

   @Inject
   private FacesContext facesContext;

   public List<String> getUsernames() {
       if (facesContext.getRenderResponse()) {
           System.out.println("Expensive call...");
       }
       List<String> usernames = new ArrayList<String>();
       usernames.add("Michal");
       usernames.add("Ilinka");
       return usernames;
   }

This part does the trick:
if (facesContext.getRenderResponse()) {
   // Expensive stuff
}

Interested in how to inject FacesContext?

You can download sample code (a Maven app) that works on JBoss 6 M5.

8 comments:

  1. Thank you very much for this!
    Helped me a lot!

    ReplyDelete
  2. Great Article android based projects

    Java Training in Chennai

    Project Center in Chennai

    Java Training in Chennai

    projects for cse

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete
  3. I went to your website and looked at the programming services you provide . I'm blown away by the services you offer since they're so unique. I would have absolutely used your services if they weren't so pricey in comparison to my budget. As a result, I believe I should compare it to other services in order to choose the finest Assignment Writing Service - Thedissertationhelp.co.uk within my budget.

    ReplyDelete
  4. The java assignments help provide a wide range of services and accept a wide range of assignments. Economics assignment assistance is frequent, but there are more possibilities such as psychology assignment assistance, accounting assignment assistance, essay assistance, and so on. These tasks are completed by specialists who hold a Ph.D. and have the necessary expertise and experience.

    ReplyDelete
  5. Most of you often put a search query as ‘who can make my assignment for me at a cheap rate in the UK?’ Here we are, offering the best service at a reasonable price.

    ReplyDelete
  6. Hasten Contracting possesses the expertise and know-how required to plan and construct CEMENT TREATED BASEpavements that are particular to the demands and requirements of each project. We collaborate closely with our clients to guarantee that the final product is of the highest quality and conforms with all applicable standards and laws.

    ReplyDelete
  7. Our company exclusively works with the most devoted, knowledgeable, and motivated optimizers while providingSearch engine optimization dubaiUnited Arab Emirates, is home to our corporate headquarters. Since we believe that every website should be an absolute delight to use, we are dedicated to directing traffic to websites that are aesthetically beautiful, practical, and simple to use.

    ReplyDelete
  8. Our exclusive design combines the strength and durability of a pump with the speed and efficiency of an electric one. In fact, it only takes half as long to transfer liquids as it does with manualhydrocarbon pumps Aberdeen pumps! And because our system is lightweight, you can use it for any kind of application. So think about switching to combinedpump today - you won't regret it!

    ReplyDelete