Book Image

Mastering JavaServer Faces 2.2

By : Anghel Leonard
Book Image

Mastering JavaServer Faces 2.2

By: Anghel Leonard

Overview of this book

Table of Contents (20 chapters)
Mastering JavaServer Faces 2.2
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
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>