Search code examples
javatemplatesexpressionstring-interpolation

Java Expression Language : Interpolation?


Greetings,

In the webapplication I am developing , I want to do something like follows:

I Have a Bean like

class Gene{
String geneid;
String sequence;
..
}

// EL expression (sometimes should be simple as "${geneid}" without URL pattern)
String exp="<a> href='http://www.ncbi.nlm.nih.gov/pubmed?term=${geneid}' />";
String outputString=someframeworkobject.somemethod(exp,aGeneInstance);

So the outputString is interpolated like : http://www.ncbi.nlm.nih.gov/pubmed?term=gene19191X

Is there any lightweight EL frameworks that I can use for this?


Solution

  • Maybe MVEL would work for you.

    With a template like

     Hello, @{person.getSex() == 'F' ? 'Ms.' : 'Mr.'} @{person.name}
    

    you can do

     context.put("person", personBean);
     String output = (String) TemplateRuntime.eval(template, context);
    

    Check out this tutorial (where I read about this, I have no experience with MVEL).