-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElClientExample.java
More file actions
62 lines (53 loc) · 1.84 KB
/
ElClientExample.java
File metadata and controls
62 lines (53 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* EL 3.0 Stand Alone API Examples
*/
package org.javaee7.chapter03.el.client;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.el.ELManager;
import javax.el.ELProcessor;
/**
*
* @author Juneau
*/
public class ElClientExample {
public static void main(String[] args) {
beanExample();
variableExample();
functionExample();
classImportExample();
}
public static void beanExample(){
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)");
System.out.println(result);
}
public static void variableExample(){
ELProcessor el = new ELProcessor();
el.setVariable("sqrt", "x -> (x * x)");
Object result = el.eval("sqrt(150)");
System.out.println(result);
}
public static void functionExample(){
ELProcessor el = new ELProcessor();
try {
el.defineFunction("org", "obtainMessage", "org.javaee7.chapter03.el.client.TestBean", "obtainMessage");
} catch (ClassNotFoundException|NoSuchMethodException ex) {
Logger.getLogger(ElClientExample.class.getName()).log(Level.SEVERE, null, ex);
}
Object result = el.eval("org:obtainMessage()");
System.out.println(result);
}
public static void classImportExample(){
ELProcessor el = new ELProcessor();
ELManager elMgr = el.getELManager();
elMgr.importClass("org.javaee7.chapter03.el.client.TestBean");
el.setVariable("bean", "new TestBean()");
Object result = el.eval("bean.num1 + bean.num2");
System.out.println(result);
}
}