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

Twitter

luni, 23 februarie 2015

[OmniFaces utilities (2.0)] Create an action listener method expression based on the given EL expression


[OmniFaces utilities] The createActionListenerMethodExpression() method creates an action listener method expression based on the given EL expression. The target method must take an ActionEvent as argument. This method returns the created action listener method expression, ready to be used as UICommand#addActionListener(javax.faces.event.ActionListener).

Method:

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

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

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 action listener method expression (save(ActionEvent evt), update(ActionEvent evt), delete(ActionEvent evt)) 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 action listener method expressions, which are provided from the CommandBean. Complete code is listed below:

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

 public void shuffle() {

  List<String> actionlist = new ArrayList();

  actionlist.add("#{commandBean.save}");
  actionlist.add("#{commandBean.delete}");
  actionlist.add("#{commandBean.update}");

  Collections.shuffle(actionlist);

  UICommand command1 = (UICommand) Faces.getViewRoot().findComponent("actionFormId:action1Id");
  UICommand command2 = (UICommand) Faces.getViewRoot().findComponent("actionFormId:action2Id");
  UICommand command3 = (UICommand) Faces.getViewRoot().findComponent("actionFormId:action3Id");
  MethodExpressionActionListener methodExpressionAL1 = Components.createActionListenerMethodExpression(actionlist.get(0));
  MethodExpressionActionListener methodExpressionAL2 = Components.createActionListenerMethodExpression(actionlist.get(1));
  MethodExpressionActionListener methodExpressionAL3 = Components.createActionListenerMethodExpression(actionlist.get(2));
  command1.addActionListener(methodExpressionAL1);       
  command2.addActionListener(methodExpressionAL2);
  command3.addActionListener(methodExpressionAL3);
  command1.getAttributes().put("value", actionlist.get(0));
  command2.getAttributes().put("value", actionlist.get(1));
  command3.getAttributes().put("value", actionlist.get(2));
 }

 public void save(ActionEvent evt){
  System.out.println("---------------------------Saving----------------------------" + evt);
 }
   
 public void delete(ActionEvent evt){
  System.out.println("---------------------------Deleting----------------------------" + evt);
 }
   
 public void update(ActionEvent evt){
  System.out.println("---------------------------Updating----------------------------" + evt);
 }
}

The below figure reveals what you will see:

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors