184 total views, 3 views today

JSF Value Expression Example

 

JSF_VALUE_EXPRESSION JSF Value Expression Example

 

In JSF page, Using EL expressions we can refer the Java bean properties and methods. Either we display a Java bean variable in UI or call a Java bean method on a button click. The EL expression supports both. The syntax of EL/Value expression is #{object.value}

As we already discussed in my previous Java Bean post, A bean consists of setter and getter methods. The SETTER methods are for inserting data and GETTER methods are for reading data. Similarly, the value expressions call those methods based on the UI component/argument they are residing. Before diving into JSF Value Expression example, Look at the following rules

To access a primitive data  property in a bean: #{user.username}  => String getUsername() 

To access an Array or ArrayList: #{ user.address[‘0’] }   => String[] getAddress() 

To access a Hash Table or Hash Map: #{ user.stocks.apple} => stocks.put(“apple”,”12121″)

To call a method: #{user.doProcess} =>  public void doProcess(){}

To call a method with an argument: #{user.doProcess(‘Govind’)} => public void doProcess(String name )

 

The Value Expression calls the SETTER or the GETTER methods based on the UI component. For Example: 

  • If you use Value expression in <h:inputText value=#{user.username}> then it calls the SETUsername() method in user bean 
  • If you use Value expression in <h:outputText value=#{user.username}> then it calls the getUsername() method in user bean

 

Let’s look at the following example.

  • The annotation @ManagedBean makes this Java Bean class into Managed Bean that I will cover in my next post 
  • The following Managed bean contains setter/Getter methods for a String, Boolean, Array[], ArrayList, HashTable data types; Two custom methods 

 

 

The Following JSF page reads the above Managed Java Bean class properties,  and the custom methods. So simple right. 

 

 

Output of the JSF page in the Browser: 

 

JSF_VALUE_EXPRESSION_EXAMPLE-1 JSF Value Expression Example

 

Tomcat console prints the following When the Method ad MethodWithArgument button is clicked

 

setUsername() called with value ‘username property’
Method Called
setUsername() called with value ‘username property’
Method with Argument called

 

Download JSF Value Expression Example Project t: JSF Value Expression Example

 

govindan JSF Value Expression Example
Connect me

Govind

Thank you for visiting my personal blog. Myself Govindan, Software Developer by profession since 2006 and hence I started this blog early in 2016 and ever since I've been writing about technologies experienced and learnings of everyday life.

The views expressed on this blog are my personal views and do not necessarily reflect the views of my employer.

Please feeling free to reach me on any comments and feedbacks you have. Would be more than glad to listen and reply šŸ™‚
govindan JSF Value Expression Example
Connect me
By | 2017-09-30T13:03:03+00:00 September 30th, 2017|Categories: JSF|Tags: , |0 Comments
Like us on Facebook.
Connect!