Wednesday, 30 March 2016

JSF-5

JSF - Expression Language

JSF provides a rich expression language. We can write normal operations using #{operation-expression} notation. Some of the advantages of JSF Expression languages are following.
  • Can reference bean properties where bean can be a object stored in request, session or application scope or is a managed bean.
  • Provides easy access to elements of a collection which can be a list, map or an array.
  • Provides easy access to predefined objects such as request.
  • Arithmetic, logical, relational operations can be done using expression language.
  • Automatic type conversion.
  • Shows missing values as empty strings instead of NullPointerException.

Example Application

Let us create a test JSF application to test expression language.
StepDescription
1Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Application chapter.
8Modify UserData.java under package com.tutorialspoint.test as explained below.
9Modify home.xhtml as explained below. Keep rest of the files unchanged.
10Compile and run the application to make sure business logic is working as per the requirements.
11Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
12Launch your web application using appropriate URL as explained below in the last step.

UserData.java

package com.tutorialspoint.test;

import java.io.Serializable;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {

private static final long serialVersionUID = 1L;

   private Date createTime = new Date();
   private String message = "Hello World!";

   public Date getCreateTime() {
      return(createTime);
   }
   public String getMessage() {
      return(message);
   }
}

home.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://java.sun.com/jsf/html">
   <h:head>
      <title>JSF Tutorial!</title>
   </h:head>
   <h2>Expression Language Example</h2>
   Creation time: 
   <h:outputText value="#{userData.createTime}"/>
   <br/><br/>Message: 
   <h:outputText value="#{userData.message}"/>
   </h:body>
</html> 
 
Once you are ready with all the changes done, let us compile and run the application as we did in JSF - First Application chapter. If everything is fine with your application, this will produce following result:

JSF Expression Language Result

JSF - Internationalization

Internationalization is a technique in which status messages, GUI component labels, currency, date are not hardcoded in the program instead they are stored outside the source code in resource bundles and retrieved dynamically. JSF provide a very convenient way to handle resource bundle.
Following steps are required to internalize a JSF application

Step 1. Define properties files

Create properties file for each locale.Name should be in <file-name>_<locale>.properties format.
Default locale can be omitted in file name.

messages.properties

greeting=Hello World!

messages_fr.properties

greeting=Bonjour tout le monde!

Step 2. Update faces-config.xml

faces-config.xml

<application>
   <locale-config>
      <default-locale>en</default-locale>
      <supported-locale>fr</supported-locale>
   </locale-config>
   <resource-bundle>
      <base-name>com.tutorialspoint.messages</base-name>
      <var>msg</var>
   </resource-bundle>
</application>

Step 3. Use resource-bundle var

home.xhtml

<h:outputText value="#{msg['greeting']}" />


























messages.properties

greeting=Hello World!

messages_fr.properties

greeting=Bonjour tout le monde!

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
   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-facesconfig_2_0.xsd"
   version="2.0">
   <application>
      <locale-config>
         <default-locale>en</default-locale>
         <supported-locale>fr</supported-locale>
      </locale-config>
      <resource-bundle>
         <base-name>com.tutorialspoint.messages</base-name>
         <var>msg</var>
      </resource-bundle>
   </application>
</faces-config>

UserData.java

package com.tutorialspoint.test;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {

   private static final long serialVersionUID = 1L;
   private String locale;

   private static Map<String,Object> countries;
   
   static{
      countries = new LinkedHashMap<String,Object>();
      countries.put("English", Locale.ENGLISH);
      countries.put("French", Locale.FRENCH);
   }

   public Map<String, Object> getCountries() {
      return countries;
   }

   public String getLocale() {
      return locale;
   }

   public void setLocale(String locale) {
      this.locale = locale;
   }

   //value change event listener
   public void localeChanged(ValueChangeEvent e){
      String newLocaleValue = e.getNewValue().toString();
      for (Map.Entry<String, Object> entry : countries.entrySet()) {
         if(entry.getValue().toString().equals(newLocaleValue)){
            FacesContext.getCurrentInstance()
               .getViewRoot().setLocale((Locale)entry.getValue());         
         }
      }
   }
}

home.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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
   <h:head>
      <title>JSF tutorial</title>   
   </h:head>
   <h:body> 
      <h2>Internalization Language Example</h2>
   <h:form>
      <h3><h:outputText value="#{msg['greeting']}" /></h3>
      <h:panelGrid columns="2"> 
         Language : 
         <h:selectOneMenu value="#{userData.locale}" onchange="submit()"
            valueChangeListener="#{userData.localeChanged}">
            <f:selectItems value="#{userData.countries}" /> 
         </h:selectOneMenu> 
      </h:panelGrid> 
   </h:form>
</h:body>
</html>
Once you are ready with all the changes done, let us compile and run the application as we did in JSF - First Application chapter. If everything is fine with your application, this will produce following result:
JSF Internationalization Result


Change language from dropdown. You will see the following output.
JSF Internationalization Result1

 


 

No comments:

Post a Comment