Java Reference
In-Depth Information
/**
* @param num1 the num1 to set
*/
public void setNum1(int num1) {
this.num1 = num1;
}
/**
* @return the num2
*/
public int getNum2() {
return num2;
}
/**
* @param num2 the num2 to set
*/
public void setNum2(int num2) {
this.num2 = num2;
}
public static String obtainMessage(){
return "Hello from the function";
}
}
Next, we'll take a look at the code that can be utilized to bring an instance of TestBean into an EL context for use
with EL expressions. To do so, an instance of ELProcessor must be obtained, and then the defineBean method is
invoked, passing the String-based variable name along with the String-based fully qualified class name of the bean
that we wish to make available. Once the bean has been made available, it can be utilized in an EL expression and
evaluated by calling the ELProcessor eval method, as demonstrated in the following code.
ELProcessor el = new ELProcessor();
// Assign a bean instance to a variable
el.defineBean("c", new TestBean());
el.setVariable("adder", "(x,y) -> x + y");
// Utilize the bean instance variables
Object result = el.eval("adder(c.num1, c.num2)");
Declaring a Variable
The stand-alone API makes it easy to declare an EL variable and assign an expression to it. This can be done by first
obtaining an instance of ELProcessor , and then calling the setVariable method, passing the String-based name of the
desired variable, along with the String-based expression. The following lines of code demonstrate this functionality by
assigning a lambda expression to a String-based variable.
ELProcessor el = new ELProcessor();
el.setVariable("sqrt", "x -> (x * x)");
Object result = el.eval("sqrt(150)");
 
Search WWH ::




Custom Search