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.
- Project Structure in Netbeans.
- The properties file is created in the class path. In the project it is created in the com.chillyfacts.com package.
You can write HTML included messages also in properties file.
message.propertiesmessage_for_name = please enter the name completely message_for_mobile = please enter a valid mobile number message1=Chillyfacts.com 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}
- 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 https://chillyfacts.com/jsf-resource-bundle-messages-properties-file-location-in-netbeans-ide/
faces-config.xml<?xml version='1.0' encoding='UTF-8'?> <faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> <application> <resource-bundle> <base-name>com.chillyfacts.com.message</base-name> <var>msgs</var> </resource-bundle> </application> </faces-config>
- web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list> </web-app>
- We can access the messages as ‘msgs.key’ since we have given a var name msgs in faces-config.
index.xhtml<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"> <h:head> <title>Facelet Title</title> </h:head> <h:body> <h:form> 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> </h:form> </h:body> </html>
- 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.
index1.xhtml<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html"> <h:head> <title>Facelet Title</title> </h:head> <h:body> 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> <h:outputFormat value="#{msgs['message.param2']}"><br></br> <f:param value="param0" /> <f:param value="param1" /> </h:outputFormat> </h:body> </html>
- Demo index.xhtml
http://localhost:8080/ResourceBundle/faces/index.xhtml - Demo index1.xhtml
http://localhost:8080/ResourceBundle/faces/index1.xhtml - Download the project here.
ResourceBundle
0 Comments
Comments
Leave a Comment