JSF Mini Book – Tips, concepts and good practices

18

Posted on September 2, 2012 by

Share it now!

Flash Scope

In a web application is not hard to find a situation where it is needed to redirect a user using HttpServletResponse.sendRedirect method. When the sendRedirect method is used the data of the first request is lost. The image below explains how the redirect works:

It is not a good practice to use the User session (HttpSession) to store objects/information, attributes or values that would be lost after a redirect. Remember that to each user that uses the HttpSession it will be more memory of the server that will be allocated.

The JSF 2.0 has a very useful component named Flash Scope. This component will keep the object until the end of the redirect. It works like a Map<String, Object> that will keep the object temporarily in the session, and after the object is used its reference will be eliminated. The code below shows how to put an object inside the flash scope:

// class import
importcom.sun.faces.context.flash.ELFlash;

// use it before the redirect
ELFlash.getFlash().put("car", carObject);

To use the value in the page after the redirect just use it through the EL: “#{flash.car.color}“. Automatically the car reference will be removed from the container memory after the utilization of the car attribute.

Other good functionality of the Flash Scope is that it is possible to keep the object in the memory; to use this feature just access the object like this: “#{flash.keep.car.color}“. Just by adding the work keep before the object will indicate to the JSF it should not eliminate the object after using it (clique here to see more about it).

Response to JSF Mini Book – Tips, concepts and good practices

  1. Arslan Afzal

    With your code lines

    and

    eclipse was giving error that:

    Multiple annotations found at this line:
    - Invalid location of text (“) in tag ().
    - Invalid location of text (=”) in tag ().
    - Element type “h:outputStylesheetlibrary” must be followed by either attribute specifications, “>”
    or “/>”.

    So I’ve corrected it by changing lines to

    But I think as you mentioned in case of versions of resources, it will automatically picks up latest version. but the code I’m using will not control the version. It looks like hard-coded.

    What you say about it?

    Moreover, You wrote:
    “To use a mapped library by the ResourceHandler let us create a folder named “index.xhtml” inside the folder “/WebContent”.”

    I think you have not created a folder name “index.xhtml” but you have created a file named “index.xhtml”. If I’m right then you need to correct it. :)

    • uaihebert Post author

      Hello Arslan, how are you?

      Unfortunately the xml are not displayed here. =/

      Could you send me an email with the code and the texts that you wrote?

      Thanks for your support and passing by.

      My email is contato (at] uaihebert (d0T) com

      Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *