Quickly apply Expression Language 3.0 API Guide in Java Library
In the Java class library, Exposition Language 3.0 (EL 3.0) API is a powerful feature that can be used to analyze and evaluate string expressions during runtime.EL 3.0 is introduced by the JavaseerVer Pages (JSP) 2.3 specification. It can be used to access and operate the JavaBean attributes, array, collection and mapping.
One of the main features of EL 3.0 is the support method call.This means that it can run in syntax similar to function calls and pass parameters.For example, we can use EL 3.0 to obtain a collection size and judge whether the string is empty.The following is a simple sample code:
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.el.ELContext;
import javax.el.MethodExpression;
import javax.el.FunctionMapper;
import javax.el.StandardELContext;
import java.util.ArrayList;
import java.util.List;
public class ELExample {
public static void main(String[] args) {
// Create an ExpressionFactory instance
ExpressionFactory factory = ExpressionFactory.newInstance();
// Create an EL context
ELContext context = new StandardELContext(factory);
// Create a list object
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
// Register the list variable in EL context
context.getVariableMapper().setVariable("list", factory.createValueExpression(list, List.class));
// Create an EL expression to obtain the size of the list
ValueExpression expression = factory.createValueExpression(context, "${list.size()}", int.class);
// Evaluate expression in EL context
int size = (int) expression.getValue(context);
System.out.println ("The size of the list is:" + size);
}
}
In the above example, first of all, we created an ExpressionFactory instance, which is the entrance to create an EL expression object.Then, we created an EL context, which is used to store the value and variables of EL expressions.Next, we created a list object and registered it into EL context.We created an EL expression `$ {list.size ()}, which is used to obtain the size of the list.Finally, we evaluate the EL expression by calling the `Expression.getValue (Context) method and print the results.
EL 3.0 API has rich functions and can be used to handle various types of expressions.It is very flexible and easy to integrate into existing Java applications.If you need to analyze and evaluate string expressions in Java applications, EL 3.0 is a powerful and convenient choice.