JSF Mini Book – Tips, concepts and good practices

Smoothing the JSF development

“ui:debug” – A component that helps in the application development with JSF is the “ui:debug”. To activate the ui:debug just add the following code in the page: “<ui:debug />“. With this component it is possible to access the object tree in the development stage. But, how can we access this values? After the page is displayed use the shortcut ctrl + shift + d. Pay attention that in the Internet Explorer, by example, will add the page in the favorites before opening the dialog box. Each browser will have an unique behavior.

“Project Stage” – Every technology has its advantages/disadvantages regarding software code development. There are situations that some code became a burden when a debug is needed.

Silent errors usually happen in the page and we do not find it until we edit the code. When we work with JSP there is a simple technique to find what is wrong with the page; just add a try/catch that wrap all the JSP code. If any exception happens we can print the error message in the console and see what is wrong.

With JSF there are configurations that can help us developing a page or even when the code is running in production environment; in JSF “stages” are applied in the project to define specific configurations.

To configure a specific stage to your application write the code bellow inside the web.xml:

<context-param>
     <param-name>javax.faces.PROJECT_STAGE</param-name>
     <param-value>Development</param-value>
</context-param>

The allowed values until today are: “Development”, “UnitTest”, “SystemTest”, “Production” (default value). Let us talk a little bit more about the Development stage that will bring us more advantages.

When the “Development” stage is set there is no need to add a “h:message” component in the page just to see if any error message will be displayed.

The MyFaces implementation, by example, when the stage is set to Development it will do the deploy of the Javascript files individually, and it will be easier to find an error because the code will be separated in different files. But when the stage is set to Production it does the deploy of all Javascript in one file to optimize the user page rendering.

“REFRESH_PERIOD” – When we work with web technology it might happen that after editing an web page source code, this editing will not be displayed in the page. No matter how many times we edit the file the new added code to the edited file will not be displayed in the page.

JSF has a configuration that indicates to the container that it must search for any kind of updates in the pages. In the code below is written how to add this configuration that is measured by seconds:

<context-param>
    <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
    <param-value>2</param-value>
</context-param>

Add the configuration above in the web.xml and the container will have a scheduled task to check if any file were updated.

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

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

    • 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 Comment