Expression Language 3.0 API Introduction and its use in the Java class library
Expression Language (expression language) is a commonly used technology in the Java library that is used to evaluate and handle the form of string in the form of runtime.Expression Language 3.0 API is a version introduced in Java Ee 7, which provides a flexible and easy -to -use way to handle dynamic expression.This article will introduce the basic concepts and usage of Expression Language 3.0 API, and provide some Java code examples to help readers understand how they use.
In Java, the expression is usually to dynamically calculate or access data at runtime.Expression Language (EL) API allows developers to analyze and value the expression to introduce this flexibility into their applications.Expression Language 3.0 API provides a standard expression language, which can be operated through simple grammar and operating symbols, which is very suitable for use in the Java class library.
To use the Expression Language 3.0 API in the Java library, first of all, the relevant class in the `javax.el` package is needed.Then, you can analyze and value the value expression by creating a `javax.el.expressionFactory` object.The following is an example for analytical expressions:
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
public class Main {
public static void main(String[] args) {
// Create an expression factory
ExpressionFactory factory = ExpressionFactory.newInstance();
// Define a expression
String expression = "${name}";
// Create a value expression
ValueExpression valueExpression = factory.createValueExpression(expression, String.class);
// Set the variable value of the expression
valueExpression.setValue(factory.getELContext(), "name", "张三");
// Solve the expression
Object result = valueExpression.getValue(factory.getELContext());
// Output results
System.out.println(result);
}
}
In the above examples, first created an expression factory object `ExpressionFactory`, and then defined an expression` $ {name} `.Next, create a value expression object in the `CreateValueExpression` method` valueExpression`.Then, use the `setValue` method to set the variable value of the expression, and use the` GetValue` method to solve the expression.Finally, the result is output to the console.
In addition to simple value expression, Expression Language 3.0 API also supports more complicated expression, such as method calls, arithmetic operations, logical operations, etc.The following is a sample code called by method call:
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
public class Main {
public static void main(String[] args) {
ExpressionFactory factory = ExpressionFactory.newInstance();
// Define a expression
String expression = "#{person.getName()}";
// Create a method expression
MethodExpression methodExpression = factory.createMethodExpression(factory.getELContext(), expression, String.class, new Class<?>[] {});
// Create a Person object
Person Person = New Person ("Zhang San");
// Solve the expression and get the result
Object result = methodExpression.invoke(factory.getELContext(), person);
// Output results
System.out.println(result);
}
}
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
In the above example, first defines an expression `#{person.getName ()}`, it calls the `getName` method of the` Person` object.Then, use the `createmethodexpression` method to create a method expression, and pass the` Person` object as a parameter to the `Invoke` method to solve the expression.
Expression Language 3.0 API provides a convenient and powerful way to handle dynamic expression for Java developers.By understanding EL's basic concepts and use methods, developers can easily use this technology in their applications to achieve more flexible and dynamic functions.