My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...









[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians


duminică, 22 februarie 2015

[OmniFaces utilities (2.0)] Create a method expression based on the given EL expression, the given return type and the given parameter types

[OmniFaces utilities] The createMethodExpression() method creates a method expression based on the given EL expression, the given return type and the given parameter types, if any. This method returns the created method expression, ready to be used as UICommand#setActionExpression(MethodExpression).


When you need to create a void method expression, you can use a shortcut of the above OmniFaces utilities:


In OmniFaces documentation you can find the below list of examples. These should be enough to understand how to use the createMethodExpression() method.

Create a method expression based on the given EL expression, the given return type and the given parameter types, if any. As an example, the following action method examples:

public void submit1()
public String submit2()
public void submit3(String argument)
public String submit4(String argument)
public void submit5(String argument1, Long argument2)
public String submit6(Long argument1, String argument2)

can be created as follows:

createMethodExpression("#{bean.submit1}", Void.class);
createMethodExpression("#{bean.submit2}", String.class);
createMethodExpression("#{bean.submit3('foo')}", Void.class, String.class);
createMethodExpression("#{bean.submit4('foo')}", String.class, String.class);
createMethodExpression("#{bean.submit5('foo', 0)}", Void.class, String.class, Long.class);
createMethodExpression("#{bean.submit6(0, 'foo')}", String.class, Long.class, String.class);

Per example, we can programmatically assign the actions to a set of buttons (UICommand). First we have created the below JSF snippet:

 <h:form rendered="#{!facesContext.postback}">           
  <h:commandButton value="Shuffle Action" action="#{commandBean.shuffle()}"/>
 <h:form id="actionFormId" rendered="#{facesContext.postback}">           
  <h:commandButton id="action1Id"/>
  <h:commandButton id="action2Id"/>
  <h:commandButton id="action3Id"/>

Notice that we have two forms. The first one contains a button, Shuffle Action. When this button is clicked, we will programmatically and randomly assign three different actions (three method expressions: save(), update(), delete()) to the buttons from second form. Moreover, we will set the values for each button. Each time you will run the application, these three buttons will have different actions, which are provided from the CommandBean. Complete code is listed below:

import org.omnifaces.util.Components;
import org.omnifaces.util.Faces;
public class CommandBean implements Serializable {

 public void shuffle() {
  List<String> actionlist = new ArrayList();



  UICommand command1 = (UICommand) Faces.getViewRoot().findComponent("actionFormId:action1Id");
  UICommand command2 = (UICommand) Faces.getViewRoot().findComponent("actionFormId:action2Id");
  UICommand command3 = (UICommand) Faces.getViewRoot().findComponent("actionFormId:action3Id");
  MethodExpression methodExpression1 = Components.createMethodExpression(actionlist.get(0), Void.class);
  MethodExpression methodExpression2 = Components.createMethodExpression(actionlist.get(1), Void.class);
  MethodExpression methodExpression3 = Components.createMethodExpression(actionlist.get(2), Void.class);
  command1.getAttributes().put("value", actionlist.get(0));
  command2.getAttributes().put("value", actionlist.get(1));
  command3.getAttributes().put("value", actionlist.get(2));

 public void save(){
 public void delete(){
 public void update(){

The below figure reveals what you will see:

Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors