miercuri, 11 martie 2015

[OmniFaces utilities (2.0)] Programmatically evaluate/set the given EL expression


[OmniFaces utilities] The evaluateExpressionGet() method programmatically evaluate the given EL expression and return the evaluated value.
[OmniFaces utilities] The evaluateExpressionSet() method programmatically evaluate the given EL expression and set the given value.

Method:
Usage:

Let's prepare something:

public class LoginBean implements Serializable {
   
 private String email;     // suppose it will be 'rafa@rg.com'
 private String password;  // suppose it will be 'numberonealltimes'
 private String reverse;   // it will store the password reversed string

 public String loginAction(String role, String user){
  return "Welcome, " + user + "(" + role + ")!";
 }

 public String logoutAction(){
  return "Successfully logout";
 }

 //getters and setters
}

Now, let's evaluate the email and password:

import org.omnifaces.util.Faces;
...
// evaluated to 'rafa@rg.com'
String getemail = Faces.evaluateExpressionGet("#{loginBean.email}");

// evaluated to 'numberonealltimes'
String getpassword = Faces.evaluateExpressionGet("#{loginBean.password}");

// evaluated to 'Welcome, rafa@rg.com(gold)!'
String login = Faces.evaluateExpressionGet("#{loginBean.loginAction('gold','"+getemail+"')}");

// evaluated to 'Successfully logout'
String logout = Faces.evaluateExpressionGet("#{loginBean.logoutAction()}");

OmniFaces also provides an OmniFaces utilities for setting the value.

Method:
Usage:

// set 'email' field as 'rafa@rg.com'
Faces.evaluateExpressionSet("#{loginBean.email}", "rafa@rg.com");

// set 'password' field as 'numberonealltimes'
Faces.evaluateExpressionSet("#{loginBean.password}", "numberonealltimes");

// set 'reverse' field as reverse string of evaluated 'password'
Faces.evaluateExpressionSet("#{loginBean.reverse}",  new StringBuffer((String)Faces.evaluateExpressionGet("#{loginBean.password}")).reverse().toString());

Niciun comentariu:

Trimiteți un comentariu