English 中文(简体)
JSF - Expression Language
  • 时间:2024-12-22

JSF - Expression Language


Previous Page Next Page  

JSF provides a rich expression language. We can write normal operations using #{operation-expression} notation. Following are some of the advantages of JSF Expression languages.

    Can reference bean properties where bean can be an object stored in request, session or apppcation scope or is a managed bean.

    Provides easy access to elements of a collection which can be a pst, map or an array.

    Provides easy access to predefined objects such as a request.

    Arithmetic, logical and relational operations can be done using expression language.

    Automatic type conversion.

    Shows missing values as empty strings instead of NullPointerException.

Example Apppcation

Let us create a test JSF apppcation to test expression language.

Step Description
1 Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Apppcation chapter.
2 Modify UserData.java under package com.tutorialspoint.test as explained below.
3 Modify home.xhtml as explained below. Keep the rest of the files unchanged.
4 Compile and run the apppcation to make sure the business logic is working as per the requirements.
5 Finally, build the apppcation in the form of war file and deploy it in Apache Tomcat Webserver.
6 Launch your web apppcation using appropriate URL as explained below in the last step.

UserData.java

package com.tutorialspoint.test;

import java.io.Seriapzable;
import java.util.Date;

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

@ManagedBean(name = "userData", eager = true)
@SessionScoped
pubpc class UserData implements Seriapzable {
   private static final long serialVersionUID = 1L;
   private Date createTime = new Date();
   private String message = "Hello World!";

   pubpc Date getCreateTime() {
      return(createTime);
   }
   
   pubpc 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>
   
   <h:body>
      <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 apppcation as we did in JSF - First Apppcation chapter. If everything is fine with your apppcation, this will produce the following result.

JSF Expression Language Result Advertisements