How to programmatically evaluate EL in a managed bean

一世执手 提交于 2019-12-06 00:03:34

If you're sitting inside the JSF context, then just use Application#evaluateExpressionGet() to programmatically evaluate a string containing EL expressions.

String unevaluatedString = convertMailTemplateToStringSomehow();
FacesContext context = FacesContext.getCurrentInstance();
String evaluatedString = context.getApplication().evaluateExpressionGet(context, unevaluatedString, String.class);
// ...

If you're not sitting inside the JSF context, then you'd need to use a standalone EL API, such as JUEL. Or, if you're already on EL 3.0 and the string represents the sole EL expression, then use the ELProcessor API.

ELProcessor el = new ELProcessor();
el.defineBean("bean", new Bean());
el.eval("bean.foo"); // Without starting #{ and ending } !
// ...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!