北京平台网站建设公司,基于php网站开发步骤,郴州最大的网络科技公司,免费的平台Spring EL与ExpressionParser 文章目录 Spring EL与ExpressionParser介绍**使用SpEL来计算评估文字字符串表达式**使用SpEL来计算评估 bean 属性 – “item.name” 介绍
Spring表达式语言(SpEL)支持多种功能#xff0c;并且可以测试这个特殊的“ExpressionParser”接口的表达…Spring EL与ExpressionParser 文章目录 Spring EL与ExpressionParser介绍**使用SpEL来计算评估文字字符串表达式**使用SpEL来计算评估 bean 属性 – “item.name” 介绍
Spring表达式语言(SpEL)支持多种功能并且可以测试这个特殊的“ExpressionParser”接口的表达式功能。
下面是两个代码片段展示了使用 Spring EL 的基本用法
使用SpEL来计算评估文字字符串表达式
ExpressionParser parser new SpelExpressionParser();
Expression exp parser.parseExpression(put spel expression here);
String msg exp.getValue(String.class); 使用SpEL来计算评估 bean 属性 – “item.name”
Item item new Item(yiibai, 100);
StandardEvaluationContext itemContext new StandardEvaluationContext(item);//display the value of item.name property
Expression exp parser.parseExpression(name);
String msg exp.getValue(itemContext, String.class);举几个例子来测试使用SpEL
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;public class App {public static void main(String[] args) {ExpressionParser parser new SpelExpressionParser();//literal expressions Expression exp parser.parseExpression(Hello World);String msg1 exp.getValue(String.class);System.out.println(msg1);//method invocationExpression exp2 parser.parseExpression(Hello World.length()); int msg2 (Integer) exp2.getValue();System.out.println(msg2);//Mathematical operatorsExpression exp3 parser.parseExpression(100 * 2); int msg3 (Integer) exp3.getValue();System.out.println(msg3);//create an item objectItem item new Item(yiibai, 100);//test EL with item objectStandardEvaluationContext itemContext new StandardEvaluationContext(item);//display the value of item.name propertyExpression exp4 parser.parseExpression(name);String msg4 exp4.getValue(itemContext, String.class);System.out.println(msg4);//test if item.name yiibaiExpression exp5 parser.parseExpression(name yiibai);boolean msg5 exp5.getValue(itemContext, Boolean.class);System.out.println(msg5);}
}public class Item {private String name;private int qty;public Item(String name, int qty) {super();this.name name;this.qty qty;}//...
}输出结果
Hello World