views:

187

answers:

3

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?

+1  A: 

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.
Chris Dail
MessageFormat does not do expressions, though. I suppose you could combine MessageFormat with an expression language, like JUEL.
Thilo
i need more dynamic solution,because above 'Gene' bean is just an example.I use many Bean classes and need something like BeanUtils to process the field values.
umanga
+1  A: 

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

Thilo
A: 

This could be the one that you're looking for: http://www.koders.com/java/fid39C2E8453EEC56A6A2A9DFC5FFE7F6E4F7066527.aspx

I happen to be looking for this too. Hope it helps. :)

Tai Li