EL表示式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解你的问题了。下面是一个基于el表达的Java工具类,用于模板参数替换。 ```java import javax.el.ExpressionFactory; import javax.el.ValueExpression; public class ELUtils { /** * 替换el表达中的参数 * @param template 带有el表达的模版字符串 * @param params 参数集合 * @return 替换后的字符串 */ public static String replaceParams(String template, Object... params) { ExpressionFactory factory = ExpressionFactory.newInstance(); ValueExpression ve = factory.createValueExpression(new ParamsELResolver(params), template, String.class); return (String) ve.getValue(null); } /** * 自定义EL解析器,用于解析参数 */ private static class ParamsELResolver extends javax.el.ELResolver { private final Object[] params; public ParamsELResolver(Object[] params) { this.params = params; } @Override public Object getValue(javax.el.ELContext context, Object base, Object property) { if (params != null && property != null) { int index = Integer.parseInt(property.toString()); if (index >= 0 && index < params.length) { context.setPropertyResolved(true); return params[index]; } } return null; } // 其他方法可不实现 } } ``` 使用方: ```java String template = "Hello, ${0}! You are ${1} years old."; String result = ELUtils.replaceParams(template, "Tom", 25); System.out.println(result); // Hello, Tom! You are 25 years old. ``` 在模版字符串中,使用`${index}`表示使用第`index`个参数进行替换。在`replaceParams`方法中,通过自定义的EL解析器将参数传入表达中进行替换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值