Expression Language 3.0 API's application and practice in the Java class library
Expression Language 3.0 (referred to as EL) is an expression language introduced in the Java Server Pages (JSP) specification.EL provides a simple and powerful way to access and operate the attributes and execution methods of the Java object.In the Java library, EL is widely used in various web frameworks and applications to facilitate developers to process dynamic data and expressions.
EL was designed to simplify the access of Java object attributes in JSP development.In EL, use a unified expression syntax to read and write the attribute value of the Java object to avoid tedious Java code.In addition, EL can perform simple logical expression and arithmetic operations, as well as methods to call Java objects.
In the Java class library, EL can be implemented by using the class and interfaces in the javax.el bag.Here are some common EL usage and practice examples:
1. Access object attributes:
EL provides a simple way to access the attributes of the Java object.Use the point number (.) In EL expressions to represent attribute access.For example, assuming that there is a Person object called "Person", you can use EL expression `$ {person.name} to access the value of its name property.
2. Executive operational operation:
EL supports basic arithmetic operations, such as addition, subtraction, multiplication and removing method.These computing symbols can be used in EL expressions for mathematical calculations.For example, `$ {2 + 3}` will return 5.
3. Expressing logic expression:
EL can perform logical operations, such as (&&), (||) and non -(!).By using these operators in EL expressions, conditional judgments can be performed.For example, `$ {Age> 18 && Isstudent}` will return a Boolean value, indicating whether the age is greater than 18 and whether it is a student.
4. Calling method:
EL can call the Java object method.By using the method in EL expressions, you can execute the method on the object and return the result.For example, `$ {Person.getName ()}` will call the getName () method on the person object and return its results.
EL also supports functions such as accessing sets, calling static methods, and conditioning operators.It is widely used in various web frameworks in the Java library, such as Spring MVC, Apache Struts, and other Java applications.
Below is a simple example code, which demonstrates the application of EL in the Java class library:
import javax.el.ExpressionFactory;
import javax.el.StandardELContext;
import javax.el.ValueExpression;
public class ELExample {
public static void main(String[] args) {
Person person = new Person("John", 25);
// Create an EL expression factory
ExpressionFactory factory = ExpressionFactory.newInstance();
// Create EL context
StandardELContext context = new StandardELContext(factory);
// Set variables in EL context
context.setVariable("person", factory.createValueExpression(person, Person.class));
// Create EL expression
ValueExpression expression = factory.createValueExpression(context, "${person.name}", String.class);
// Execute EL expression
String name = (String) expression.getValue(context);
System.out.println("Person name: " + name);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
The above code demonstrates how to use the EL expression to obtain the value of the name property from the Person object.First, create an ExpositionFactory instance to create EL expressions.Then create a StandardelContext to set variables and execute expressions.A variable called "Person" is set in EL context, and its value is the Person object.Next, create EL expressions with the CreateValueExpression method of ExpressionFactory to create EL expressions and provide expression string and target types.Finally, the EL expression is executed by calling the getValue method, and the result is converted to the String type.
In summary, Expression Language 3.0 API provides developers with a convenient and flexible way to access and operate the attributes of the Java objects in the Java class library, as well as execution logic and operations.It is widely used in various web frameworks and applications, and can be implemented through the class and interfaces in the javax.el package.