前言

一定要多看官方文档,网上找的都是东抄西抄,而且写的很皮毛
http://juel.sourceforge.net/guide/start.html

依赖

  1. <dependency>
  2. <groupId>javax.el</groupId>
  3. <artifactId>javax.el-api</artifactId>
  4. <version>3.0.0</version>
  5. </dependency>
  6. <!-- juel -->
  7. <dependency>
  8. <groupId>de.odysseus.juel</groupId>
  9. <artifactId>juel-api</artifactId>
  10. <version>2.2.7</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>de.odysseus.juel</groupId>
  14. <artifactId>juel-impl</artifactId>
  15. <version>2.2.7</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>de.odysseus.juel</groupId>
  19. <artifactId>juel-spi</artifactId>
  20. <version>2.2.7</version>
  21. </dependency>

常见用法

  1. @Test
  2. public void testGetStarted() {
  3. //step1
  4. ExpressionFactory factory = new ExpressionFactoryImpl();
  5. SimpleContext context = new SimpleContext();
  6. //step2
  7. context.setVariable("var1", factory.createValueExpression("Hello", String.class));
  8. context.setVariable("var2", factory.createValueExpression("World", String.class));
  9. //step3
  10. String s = "{\"argIn1\":\"${var1}\",\"argIn2\":\"${var2}\"}";
  11. ValueExpression e = factory.createValueExpression(context, s, String.class);
  12. System.out.println(e.getValue(context));// --> {"argIn1":"Hello","argIn2":"World"}
  13. }
  1. @Test
  2. public void testResolveBean() {
  3. // 启用对象的方法调用
  4. // 启用NULL属性
  5. // 启用忽略类型不一致产生的异常
  6. Properties properties = new Properties();
  7. properties.setProperty(ExpressionFactoryImpl.PROP_METHOD_INVOCATIONS, Boolean.TRUE.toString());
  8. properties.setProperty(ExpressionFactoryImpl.PROP_NULL_PROPERTIES, Boolean.TRUE.toString());
  9. properties.setProperty(ExpressionFactoryImpl.PROP_IGNORE_RETURN_TYPE, Boolean.TRUE.toString());
  10. ExpressionFactory factory = new ExpressionFactoryImpl(properties);
  11. Map<String, Object> extMap = Maps.newHashMap();
  12. extMap.put("foo", "bar");
  13. ActivityEntity activityEntity = ActivityEntity.builder()
  14. .title("我的")
  15. .tenantId(0L)
  16. .id(2001L)
  17. .creator("jack")
  18. .misName("杰克")
  19. .modifier("sam")
  20. .accompanys("mis/misName")
  21. .createdTime(new Date())
  22. .extendInfo(extMap)
  23. .build();
  24. ActivityDetailAr bean = ActivityDetailAr.builder()
  25. .activityId(2001L)
  26. .activityEntity(activityEntity)
  27. .activityPersons(Lists.newArrayList(PersonRecordValue.builder().personName("威尔").build()))
  28. .build();
  29. SimpleContext context = new SimpleContext(new SimpleResolver());
  30. // resolve bean property
  31. context.setVariable("activityModel", factory.createValueExpression(bean, ActivityDetailAr.class));
  32. context.setVariable("activityEntity", factory.createValueExpression(bean.getActivityEntity(), ActivityEntity.class));
  33. System.out.println(factory.createValueExpression(context, "${activityModel.activityId}", long.class).getValue(context));
  34. System.out.println(factory.createValueExpression(context, "${activityModel.tenantId()}", String.class).getValue(context));
  35. System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity}", ActivityEntity.class).getValue(context));
  36. System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity.id}", Long.class).getValue(context));
  37. System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity.title}", String.class).getValue(context));
  38. System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity.creator}", String.class).getValue(context));
  39. System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity.accompanys}", String.class).getValue(context));
  40. System.out.println(factory.createValueExpression(context, "${activityEntity.title}", String.class).getValue(context));
  41. System.out.println(factory.createValueExpression(context, "${activityEntity.misName}", String.class).getValue(context));
  42. System.out.println(factory.createValueExpression(context, "${activityEntity.extendInfo}", Map.class).getValue(context));
  43. System.out.println(factory.createValueExpression(context, "${activityEntity.extendInfo.foo}", String.class).getValue(context));
  44. System.out.println(factory.createValueExpression(context, "${activityModel.activityPersons}", Object.class).getValue(context));
  45. System.out.println(factory.createValueExpression(context, "${activityEntity.visitTime.time}", Long.class).getValue(context));
  46. System.out.println(factory.createValueExpression(context, "${activityEntity.createdTime.time}", Long.class).getValue(context));
  47. System.out.println(factory.createValueExpression(context, "${activityEntity.modifyTime.time}", Long.class).getValue(context));
  48. }

执行结果示例:

  1. 2001
  2. 0
  3. ActivityEntity(id=2001, tenantId=0, status=null, mis=null, misName=杰克, version=null, creator=jack, createdTime=Thu May 19 12:49:47 CST 2022, modifier=sam, modifyTime=null, daXiangCopyValue=null, visitTime=null, title=我的, visitCity=null, accompanys=mis/misName, visitInfo=null, imgUrls=null, reason=null, projectId=null, personValues=null, mainOrgValues=null, extendInfo={foo=bar})
  4. 2001
  5. 我的
  6. jack
  7. mis/misName
  8. 我的
  9. 杰克
  10. {foo=bar}
  11. bar
  12. [PersonRecordValue(tenantId=null, personId=null, personName=威尔, mainOrgId=null, mainOrgName=null, mobile=null, wechat=null, creator=null, position=null, supportOrgName=null, supportOrgName1=null, supportOrgName2=null)]
  13. 0
  14. 1652935787812
  15. 0