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).

Method:

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

Usage:

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: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 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;
...
@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");
  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.setActionExpression(methodExpression1);       
  command2.setActionExpression(methodExpression2);
  command3.setActionExpression(methodExpression3);
  command1.getAttributes().put("value", actionlist.get(0));
  command2.getAttributes().put("value", actionlist.get(1));
  command3.getAttributes().put("value", actionlist.get(2));
 }

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

The below figure reveals what you will see:

Niciun comentariu:

Trimiteți un comentariu