JSF Mini Book – Tips, concepts and good practices

Internationalization as good practice

Some developers may have the following though about Internationalization: “My project will be accessed only by users of my company with my native language, why should I apply the Internationalization in my project?” The internationalization file, that will contain all messages of the system, will not only help to create a new language to the project; with this file a developer will avoid that the message (e.g. “Successful!”) to be spread several times across the system.

When a developer puts all messages inside a file any change of terminology of the project will be easier and less aggressive. Imagine a project where the word Car it is used like 50 times and there is a new task that is to change the word Car by Automobile. Without a file that contains all the project messages a developer would search all the pages for the Car word. At the end of this task the developer would be tired and his work prone to errors.

Other advantage of using a file with all project messages is that if after deploying a new version a wrong message is detected, it would not be needed the creation of a new deploy version. The easiest and fastest solution would be editing the message file inside the EAR/WAR.

To create a file with all the messages of the project add the code below to the faces-config.xml:

<application>
	<resource-bundle>
		<base-name>messages</base-name>
		<var>msgs</var>
	</resource-bundle>
</application>

The value found in the “<base-name>” key indicates the file where the messages can be found; the file must have its extension of the “.properties” type that, in the case above, would be “message.properties”. This file must be put inside the src folder of the project, the same folders that hold the Java classes. It is possible to create any number of folders or files to store different messages types. The value found in the <var> tag indicates how we will invoke the file from the view. In the code below we will be able to see how a message file would look like:

car=Car
carDoor=Door
carColor=Color

To access any value of the file described above just use the EL like: “#{msgs.car}”. It is very easy to use, reduce the repetition of the words and help the developer when a refactoring is needed.

Other advantage of using a file with all messages is that we can override the default messages of the JSF. If the attribute required=“true” is found in an inputText and the form is submitted with this field empty the following message will be displayed: “ValidationError: Value is required”. This strange message could make the user uncomfortable with application.

To overwrite the default JSF messages in the file messages of your project, just add the code below to the faces-config.xml:

<application>
	<resource-bundle>
		<base-name>messages</base-name>
		<var>msgs</var>
	</resource-bundle>
	<message-bundle>messages</message-bundle>
</application>

Now it is possible to see a tag named “<message-bundle>” in the faces-config file; this tag indicates to the JSF to search in the “message.properties” file for overwritten default messages.

Add the key “javax.faces.component.UIInput.REQUIRED=” to your message file and chose a message to this key. The message found in the “javax.faces.component.UIInput.REQUIRED=” key will be used to every required field in the system.

It is possible to find every JSF default message in the JSF specification (2.5.2.4).

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