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