开发者

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 l开发者_如何转开发ightweight EL frameworks that I can use for this?


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).


It sounds like all you need is the core Java library class MessageFormat. It is pretty easy to use and allows you to do replacement of templates in a string.

String outputString = MessageFormat.format("<a> href='http://www.ncbi.nlm.nih.gov/pubmed?term={0}' />", "gene19191X");

You can also create an instance of MessageFormat and reuse that with different values.

Other options you could also try are:

  • Apache Commons EL - This is built for expressions in web applications.
  • Groovy GStrings - I use this sometimes to evaluate a 'script'. This has the advantage of allowing more complex logic.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜