4

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.

How can I do that?

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
Luca Molteni
  • 5,091
  • 5
  • 32
  • 40

1 Answers1

11

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 } !
// ...
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
  • Do you know if it's possible to use the seam context? So that I DI object in the template. Something like http://blog.chintoju.com/2011/02/evaluate-an-el-expression.html – Luca Molteni Oct 23 '12 at 07:29
  • Seam has a simple way of evaluating templates with embedded EL expressions: `Interpolator.instance().interpolate()` method takes a string and interpolates any EL expressions it contains. Additionally, it can also interpolate up to 10 parameters you pass into the interpolator: `Interpolator.instance().interpolate("{0}#{' '}{1}", "foo", "bar");` returns `foo bar`. – EmirCalabuch Oct 23 '12 at 15:57