Press enter to see results or esc to cancel.

JSF and Resource Bundles example – JSF Tutorial Part 8

This tutorial shows the use of Resource bundle in JSF to display messages in pages. Its good to main all messages in Properties file instead of hard coding all messages in JSF Page directly. You can see the Youtube video step by step how to create this project. I have shown both writing messages in JSF page and in Message Properties file.

  1. Project Structure in Netbeans.
  2. The properties file is created in the class path. In the project it is created in the package.
    You can write HTML included messages also in properties file.

    message_for_name = please enter the name completely 
    message_for_mobile = please enter a valid mobile number
    message2=<h2 style="color: pink">Chillyfacts</h2>
    message.test1=this is message test1
    message.param1 = Chillyfacts is "message.param1" - {0}
    message.param2 = Chillyfacts is "message.param2" - {0} and {1}
  3. Configure the Properties file in Faces config file as below. We can call the message variables with the reference we give in the tags. In our example it is ‘msgs’. See this post how to create properties file

    <?xml version='1.0' encoding='UTF-8'?>
    <faces-config version="2.2"
  4. web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-name>Faces Servlet</servlet-name>
  5. We can access the messages as ‘msgs.key’ since we have given a var name msgs in faces-config.

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns=""
        <title>Facelet Title</title>
           Enter Name : <h:inputText required="true" requiredMessage="#{msgs.message_for_name}"></h:inputText>       <br></br>
           Enter Mobile : <h:inputText required="true" requiredMessage="#{msgs.message_for_mobile}"></h:inputText> <br></br>
           <h:commandButton value="Submit"></h:commandButton>
  6. Different methods though which we can access the messages is shown below.
    You can use escape=”false” as shown below to load the HTML with the message.

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns=""
             <title>Facelet Title</title>
             Hello from Facelets
             <h1 style="color: red">#{msgs.message1}</h1>
             <h:outputText value="#{msgs.message2}" escape="false" />
             <h:outputText value="#{msgs['message.test1']}" /><br></br>
             <h:outputFormat value="#{msgs['message.param1']}"><br></br>
               <f:param value="param0" />
             <h:outputFormat value="#{msgs['message.param2']}"><br></br>
               <f:param value="param0" />
               <f:param value="param1" />
  7. Demo index.xhtml

  8. Demo index1.xhtml

  9. Download the project here.


Leave a Comment