问题描述
我想在基于Seam/JSF的应用程序中添加一种简单的模板语言,以使用户编写自己的电子邮件.
I would like to add a simple template language to my application based on Seam / JSF to let the users compose their own email.
由于我不想创建新的解析器,因此我想使用统一表达式语言自行设置上下文.
Since I don't want to create a new parser, I would like to use the Unified Expression Language setting the context all by myself.
我该怎么做?
推荐答案
如果您坐在JSF上下文中,则只需使用 Application#evaluateExpressionGet()
进行编程评估包含EL表达式的字符串.
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);
// ...
如果您不在JSF上下文中,则需要使用独立的EL API,例如 JUEL .或者,如果您已经使用EL 3.0,并且该字符串代表唯一的EL表达式,则使用 ELProcessor
API.
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 } !
// ...
这篇关于如何以编程方式评估托管bean中的EL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!