JSF Mini Book – Tips, concepts and good practices

JSF 2.2 new features

Below are some of the new features of the JSF 2.2:

  • Tag “viewAction” for Restful pages – JSF 2.1 can handle a URL request by “get” with parameters in the URL. With JSF 2.2 it will be possible to invoke an action when the page is accessed. Check below in the Code 6 how is easy to get a parameter from the URL. To pass the parameter from URL to the ManagedBean a just invoke the page like: “http://localhost:8080/App/values.xhtml?carId=33”. The invoked method could return a String to start navigation. This link invocation can be done from outside the JSF realm. Too see more information about how to pass parameters in a URL with JSF 2.1 click here.
  • Ajax queue – to avoid an impatient user to send the same Ajax request several times, the “f:ajax” will come with an attribute named “delay”. The default value is this attribute is 300ms and if the user clicks in the button more than once, just the last Ajax request will be sent. It is possible to disable this option by writing delay=“none”.
  • Upload – if your application needs a file upload function, it would be necessary to create a Servlet or use some JSF component (Primefaces, Richfaces, etc.). With JSF 2.2 the upload with be native; check the Code 7 to see how it will be.
  • Injections to all JSF components – with JSF 2.2 will be possible to use injection in Validators, Converters, etc. In version 2.1 the injection is only allowed inside a ManagedBean. If a developer needs a @EJB, for example inside a Converter, a JNDI lookup would be needed.
  • Declaring a component – It will not be necessary to use the xml to declare the components JSF. With the tag @FacesComponent will be possible to declare the components inside the class.
  • Cross Site Request Forgery – it is a hacker attack that uses the session of a valid logged user to run actions in the container. JSF 2.0 will have an extra protection against this kind of attack. The JSF will save a token and will check it at every request received from that user.
  • Collection on Datatable – today the datatable does not accept a collection of items using the Collection interface. Today the JSF datatable accepts only Array[], List<?> or a dataModel. JSF 2.2 introduces a new class named javax.faces.model.CollectionDataModel where it wraps any kind of collection. The datatable behavior will be like this: if the given collection is a List then a ListDataModel will be used, if it is not a List the new class will get in action.

Code 6

    <f:viewParam name="car" value="#{carMB.id}"/>
    <f:viewAction action="#{carMB.checkCarId}"/>

Code 7

<h:form prependId="false" enctype="multipart/form-data">
    <!--JSF new component -->
    <h:inputFile id="fileUpload" value="#{someBean.file}" />
    <h:commandButton value="Upload" />

18 thoughts on “JSF Mini Book – Tips, concepts and good practices

  1. Really cool material! I agree with Tim. If this could be distributed in pdf format, you will become a little more famous. :) Thanks, good job!

  2. I’m not able to see this library applied in my program.
    Output is: Real Madrid, Champion!!! (No image and style)

    Please let me know what I’m missing.

  3. I was trying to learn Spring MVC for last 2 months, Every time i try to start, i get stuck somewhere. 2 Months passed, and i did not learn anything. I purchased two books for spring. Finally i quit Java web programming. One day I thought to give a try to JSF. and found some tutorials online, worked on them and I loved it. Your tutorials are going to help me a lot.
    Thank you so much.
    I would like to tell people here, don’t go near spring, It’s a piece of crap.

    • Arslan,

      Thanks for the feedback.

      My next post will be a full web application with Spring MVC.

      Maybe this application will help you understanding better the Spring, it is a good tool! =D

      See you later.

  4. With your code lines


    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. :)

    • 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


Leave a Comment