前言
一定要多看官方文档,网上找的都是东抄西抄,而且写的很皮毛
http://juel.sourceforge.net/guide/start.html
依赖
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- juel -->
<dependency>
<groupId>de.odysseus.juel</groupId>
<artifactId>juel-api</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>de.odysseus.juel</groupId>
<artifactId>juel-impl</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>de.odysseus.juel</groupId>
<artifactId>juel-spi</artifactId>
<version>2.2.7</version>
</dependency>
常见用法
@Test
public void testGetStarted() {
//step1
ExpressionFactory factory = new ExpressionFactoryImpl();
SimpleContext context = new SimpleContext();
//step2
context.setVariable("var1", factory.createValueExpression("Hello", String.class));
context.setVariable("var2", factory.createValueExpression("World", String.class));
//step3
String s = "{\"argIn1\":\"${var1}\",\"argIn2\":\"${var2}\"}";
ValueExpression e = factory.createValueExpression(context, s, String.class);
System.out.println(e.getValue(context));// --> {"argIn1":"Hello","argIn2":"World"}
}
@Test
public void testResolveBean() {
// 启用对象的方法调用
// 启用NULL属性
// 启用忽略类型不一致产生的异常
Properties properties = new Properties();
properties.setProperty(ExpressionFactoryImpl.PROP_METHOD_INVOCATIONS, Boolean.TRUE.toString());
properties.setProperty(ExpressionFactoryImpl.PROP_NULL_PROPERTIES, Boolean.TRUE.toString());
properties.setProperty(ExpressionFactoryImpl.PROP_IGNORE_RETURN_TYPE, Boolean.TRUE.toString());
ExpressionFactory factory = new ExpressionFactoryImpl(properties);
Map<String, Object> extMap = Maps.newHashMap();
extMap.put("foo", "bar");
ActivityEntity activityEntity = ActivityEntity.builder()
.title("我的")
.tenantId(0L)
.id(2001L)
.creator("jack")
.misName("杰克")
.modifier("sam")
.accompanys("mis/misName")
.createdTime(new Date())
.extendInfo(extMap)
.build();
ActivityDetailAr bean = ActivityDetailAr.builder()
.activityId(2001L)
.activityEntity(activityEntity)
.activityPersons(Lists.newArrayList(PersonRecordValue.builder().personName("威尔").build()))
.build();
SimpleContext context = new SimpleContext(new SimpleResolver());
// resolve bean property
context.setVariable("activityModel", factory.createValueExpression(bean, ActivityDetailAr.class));
context.setVariable("activityEntity", factory.createValueExpression(bean.getActivityEntity(), ActivityEntity.class));
System.out.println(factory.createValueExpression(context, "${activityModel.activityId}", long.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityModel.tenantId()}", String.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity}", ActivityEntity.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity.id}", Long.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity.title}", String.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity.creator}", String.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityModel.activityEntity.accompanys}", String.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityEntity.title}", String.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityEntity.misName}", String.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityEntity.extendInfo}", Map.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityEntity.extendInfo.foo}", String.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityModel.activityPersons}", Object.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityEntity.visitTime.time}", Long.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityEntity.createdTime.time}", Long.class).getValue(context));
System.out.println(factory.createValueExpression(context, "${activityEntity.modifyTime.time}", Long.class).getValue(context));
}
执行结果示例:
2001
0
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})
2001
我的
jack
mis/misName
我的
杰克
{foo=bar}
bar
[PersonRecordValue(tenantId=null, personId=null, personName=威尔, mainOrgId=null, mainOrgName=null, mobile=null, wechat=null, creator=null, position=null, supportOrgName=null, supportOrgName1=null, supportOrgName2=null)]
0
1652935787812
0