Understand the technical principles of the Jakarta Expression Language API framework in the Java class library S)
Learn about the technical principles of Jakarta Expression Language (EL) API framework in the Java Class Library
Introduction:
Jakarta Expression Language (EL) API is part of the Javaee standard, which aims to provide developers with a simple expression language that dynamically access and operates the JavaBean attribute at runtime.This article will introduce the technical principles and usage of the Jakarta El API framework, and some Java code examples.
Technical principle:
The technical principle of Jakarta Expression Language (EL) API framework is based on Java's reflection mechanism.The core is to analyze the expression as an executable code, and dynamically access and operate the JavaBean property by reflected at runtime.The following are some key concepts and technical principles of the Jakarta El API framework:
1. Expressive analysis: Jakarta El API uses expression language to describe access and operation of JavaBean property.These expressions can contain the names, method calls and arithmetic, and logic of the attribute.The framework will analyze the expression as an executable code.
2. EL context: EL context is an environment for expression evaluation.It provides variables and functions related to expression.In the Javaee application, EL context can include requests and session attributes, application range attributes, and predefined EL variables and functions.
3. Analyst: Jakarta El API provides parsers to parse the expression and generate the corresponding executable code.The parser is responsible for analyzing the expression as an abstract syntax tree (AST), and generates the Java code corresponding to and the expression.
4. Expressive value: Once the expression is parsed as an executable code, the framework can be used to value the expression to obtain or set the value of the JavaBean property.By reflection, the framework can be dynamically accessed and operated.
Example:
Here are some examples of the Jakarta El API framework.
1. Create EL context:
// Create EL context
ELContext elContext = new ELContext();
// Set variables in EL context
elContext.setVariable("name", "John Doe");
2. Analysis and value of value:
// Create an expression factory
ExpressionFactory expressionFactory = ExpressionFactory.newInstance();
// Analytical expression
ValueExpression valueExpression = expressionFactory.createValueExpression(elContext, "${name}", String.class);
// Value expression formula
String name = (String) valueExpression.getValue(elContext);
System.out.println (name); // Output: John Doe
3. Set the attribute value:
// Create an expression factory
ExpressionFactory expressionFactory = ExpressionFactory.newInstance();
// Analytical expression
ValueExpression valueExpression = expressionFactory.createValueExpression(elContext, "${name}", String.class);
// Set the attribute value
valueExpression.setValue(elContext, "Jane Doe");
// Get new attribute values
String newName = (String) valueExpression.getValue(elContext);
System.out.println (newName); // Output: Jane Doe
in conclusion:
By understanding the technical principles and usage methods of Jakarta Exposition Language (EL) API framework, developers can access and operate JavaBean attributes with simple expression language in Javaee applications.This provides a flexible way for dynamic attribute access and operation, and simplifies the development process.