Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering JavaServer Faces 2.2

You're reading from   Mastering JavaServer Faces 2.2 Master the art of implementing user interfaces with JSF 2.2

Arrow left icon
Product type Paperback
Published in Jun 2014
Publisher
ISBN-13 9781782176466
Length 578 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Anghel Leonard Anghel Leonard
Author Profile Icon Anghel Leonard
Anghel Leonard
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Dynamic Access to JSF Application Data through Expression Language (EL 3.0) FREE CHAPTER 2. Communication in JSF 3. JSF Scopes – Lifespan and Use in Managed Beans Communication 4. JSF Configurations Using XML Files and Annotations – Part 1 5. JSF Configurations Using XML Files and Annotations – Part 2 6. Working with Tabular Data 7. JSF and AJAX 8. JSF 2.2 – HTML5 and Upload 9. JSF State Management 10. JSF Custom Components 11. JSF 2.2 Resource Library Contracts – Themes 12. Facelets Templating A. The JSF Life Cycle
Index

EL method expressions

With EL expressions, we can call arbitrary static and public methods that live on the server side in managed beans. Such expressions are usually present in tag's attributes (that is, inside an action or actionListener attribute) and must use the deferred evaluation syntax since a method can be called during different phases of the life cycle. Commonly, methods are called to respond with actions to different kinds of events and for autopages navigation.

Let's see some examples of calling bean methods using EL (all methods were defined in the PlayersBean managed bean):

  • Calling the vamosRafa_1 void bean method with no arguments, as shown in the following code:
    public void vamosRafa_1(){
      System.out.println("Vamos Rafa!");
    }
    
    #{playersBean.vamosRafa_1()}
  • Calling the vamosRafa_2 bean method with no arguments. It returns a string, as shown in the following code:
    public String vamosRafa_2() {
      return "Vamos Rafa!";
    }
    
    #{playersBean.vamosRafa_2()}

    The returned string, Vamos Rafa!, can be displayed on the web page or used for other purposes. In other words, the expression will be evaluated to this string.

  • Calling the vamosRafa_3 bean method with one argument. It returns void, as shown in the following code:
    public void vamosRafa_3(String text) {
      System.out.println(text);
    }
    
    #{playersBean.vamosRafa_3('Vamos Rafa!')}

    Notice that the String arguments are passed by using quotes.

    Note

    The String constants are passed between simple or double quotes!

  • Calling the vamosRafa_4 bean method with two arguments. It returns a string, as shown in the following code:
    public String vamosRafa_4(String name, String surname) {    
      return "Vamos " + name + " " + surname + "!";
    }
    
    #{playersBean.vamosRafa_4(playersBean.playerName, playersBean.playerSurname)}

    The expression will be evaluated to the string, Vamos Rafael Nadal!.

  • Calling the vamosRafa_5 bean method for autonavigation. First, define the method in the managed bean to return a view (outcome) name (vamos is the view name for the vamos.xhtml file), as shown in the following code:
    public String vamosRafa_5(){
     return "vamos";
    }

Furthermore, extract the view name in the action attribute of any JSF UI component as shown in the following code:

<h:form>  
  <h:commandButton action="#{playersBean.vamosRafa_5()}" value="Vamos ..." />
</h:form>

Now, when the button labeled Vamos... is clicked, JSF will resolve the view name, vamos, to the vamos.xhtml file. Moreover, JSF will look for the vamos.xhtml file in the current directory and will navigate to it. Commonly, these navigation methods are used for conditional navigation between JSF pages.

Note

We have used parentheses to call a method, even when the method doesn't contain arguments. A special case is represented by the methods that contain an ActionEvent argument. These methods should be called without parentheses, except in the case when you override the ActionEvent argument altogether by passing and specifying custom argument(s).

EL expressions can also be used inside JavaScript function calls. For example, when you want to pass bean properties to a JavaScript function, you need to place them between quotes, as shown in the following code:

<h:form>
  <h:commandButton type="button" value="Click Me!" onclick="infoJS('#{playersBean.playerName}', '#{playersBean.playerSurname}')"/>
</h:form>

The JavaScript function for this is shown in the following code:

<script type="text/javascript">
  function infoJS(name, surname) {
    alert("Name: " + name + " Surname: " + surname);
  }
</script>
You have been reading a chapter from
Mastering JavaServer Faces 2.2
Published in: Jun 2014
Publisher:
ISBN-13: 9781782176466
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime