-
Notifications
You must be signed in to change notification settings - Fork 0
Context Object
A context object in MVEL is an optional enviromental element that can be used in conjunction with, or an alternative to, injected variables into the parser/interpreter.
String test = "Hello";
Object result = MVEL.eval("toUpperCase()", test);
In this example, we're using the test String object as our context object, allowing us to execute the toUpperCase() method as a local function in our expression. We can also access other methods in the java.util.String class as bean properties.
public class User {
private String name;
private String password;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Now, just for example purposes, lets apply this class in one of our tests:
User user = new User();
user.setName("Bob");
user.setPassword("Despot");
user.setAge(30);
String name = (String) MVEL.eval("name", user);
In this example, the value of the variable name returned by the expression will be "Bob".
In situations where both external variables and a context object are being used, the MVEL runtime will look for a variable first, and then look to see if the context object contains the identifier.
This is the documentation for MVEL 1.2.x and 1.3.x. If you are using MVEL 2.*, please proceed to the documentation for MVEL 2.*