博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SqEl基础表达式1
阅读量:4228 次
发布时间:2019-05-26

本文共 4255 字,大约阅读时间需要 14 分钟。

直接看代码

1 基本类型解析

public class LiteralExprSample {    /**     * 对基本类型解析     * @param args     */    public static void main(String[] args){        ExpressionParser parser=new SpelExpressionParser();        //解析字符串        //"'Hello world'" 或"\"Hello World\""        String helloWorld=(String)parser.parseExpression("\"Hello World\"").getValue();        String helloWorld1=(String)parser.parseExpression("'Hello World'").getValue();        //解析双精度浮点型        double doubleNumber=(Double)parser.parseExpression("6.0221415E+23").getValue();        //解析整型        int maxValue=(Integer)parser.parseExpression("0x7FFFFFFF").getValue();        //解析布尔型        boolean trueValue=(Boolean)parser.parseExpression("true").getValue();        //解析空值        Object nullValue=parser.parseExpression("null").getValue();        System.out.println(helloWorld);        System.out.println(helloWorld1);        System.out.println(doubleNumber);        System.out.println(maxValue);        System.out.println(trueValue);        System.out.println(nullValue);    }}
Hello World
Hello World
6.0221415E23
2147483647
true
null

2对象属性解析

public class PropertyExprSample {    /**     *     * 在Sqel中,可使用类似xxx.yyy.zzz的对象属性路径轻松地访问对象属性值。     * @param args     */    public static void main(String[] args){        User user=new User();        user.setUserName("tom");        user.setLastVisit(new Date());        user.setCredits(100);        user.setPlaceOfBirth(new PlaceOfBirth("中国","厦门"));        //构造Spel解析上下文        ExpressionParser parser=new SpelExpressionParser();        EvaluationContext context=new StandardEvaluationContext(user);        //基本属性值获取        String username=(String)parser.parseExpression("userName").getValue(context);        int credits=(Integer)parser.parseExpression("credits+10").getValue(context);        String city=(String)parser.parseExpression("placeOfBirth.city").getValue(context);        System.out.println(username);        System.out.println(credits);        System.out.println(city);    }}
tom
110
厦门

3数组、集合类型解析

public class CollectionExprSample {    /**     * 在Sqel中支持数组、集合类型(map,list)的解析,数组支持标准创建数组的方式,如“new int[]{1,2,3}".不支持多元数组     * list支持大括号括起来的内容,如"{1,2,3,4}" "{
{'a','b'},{'x','y'}}". * map采用如下方式:{userName:'tom',credits:100} * @param args */ public static void main(String[] args){ User user=new User(); user.setUserName("tom"); user.setLastVisit(new Date()); user.setCredits(100); user.setPlaceOfBirth(new PlaceOfBirth("中国","厦门")); ExpressionParser parser=new SpelExpressionParser(); EvaluationContext context=new StandardEvaluationContext(user); //数组表达式解析 int[] array1=(int[])parser.parseExpression("new int[]{1,2,3}").getValue(context); int[][] array2=(int[][])parser.parseExpression("new int[2][3]").getValue(context); System.out.println(array1.length); System.out.println(array2.length); //目前不支持多维数组初始化,一下语句将报错 //int[][] array3=(int[][])parser.parseExpression("new int[2][3]{
{1,2,3},{4,5,6}}").getValue(context); //System.out.println(array3); //list表达式解析 List list=(List)parser.parseExpression("{1,2,3,4}").getValue(context); List listOfLists=(List)parser.parseExpression("{
{'a','b'},{'x','y'}}").getValue(context); System.out.println(list); System.out.println(listOfLists); //列表字符串解析 Map userInfo=(Map)parser.parseExpression("{userName:'tom',credits:100}").getValue(context); List userInfo2=(List)parser.parseExpression("{
{userName:'tom',credits:100},{userName:'tom',credits:100}}").getValue(context); System.out.println(userInfo); System.out.println(userInfo2); //从数组,list,map中取值 String interest1=(String)parser.parseExpression("interestsArray[0]").getValue(context); String interest2=(String)parser.parseExpression("interestsList[0]").getValue(context); String interest3=(String)parser.parseExpression("interestsMap['interest1']").getValue(context); System.out.println(interest1); System.out.println(interest2); System.out.println(interest3); }}
3
2
[1, 2, 3, 4]
[[a, b], [x, y]]
{userName=tom, credits=100}
[{userName=tom, credits=100}, {userName=tom, credits=100}]
Hello
Hello
Hello

转载地址:http://qojqi.baihongyu.com/

你可能感兴趣的文章
Beginning Relational Data Modeling, Second Edition
查看>>
Winternals Defragmentation, Recovery, and Administration Field Guide
查看>>
Video Conferencing over IP : Configure, Secure, and Troubleshoot
查看>>
Pro ASP.NET 2.0 Website Programming
查看>>
Disassembling Code : IDA Pro and SoftICE
查看>>
Building Online Communities With Drupal, phpBB, and WordPress
查看>>
C# 2.0 : The Complete Reference
查看>>
Digital Character Animation 3
查看>>
Flash 8 Cookbook
查看>>
Mastering Mambo: E-Commerce, Templates, Module Development, SEO, Security, and Performance
查看>>
Web Design For Dummies (For Dummies
查看>>
Flash 8: Projects for Learning Animation and Interactivity
查看>>
Web Portals: : The New Gateways to Internet Information and Services
查看>>
Web Content Caching and Distribution
查看>>
Use Cases Patterns and Blueprints
查看>>
Understanding SOA with Web Services
查看>>
Unit Testing in Java: How Tests Drive the Code
查看>>
The J2EE Architect's Handbook: How to be a Successful Technical Architect for J2EE Applications
查看>>
Practical Mono (Expert's Voice in Open Source)
查看>>
Linux(R) Troubleshooting for System Administrators and Power Users
查看>>