2

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?

3
  • To add my own question: Can the JSP EL (which would be a fit here) be called from Java code? Commented Jan 20, 2010 at 3:32
  • I came across with Apache Common JEXL and Commons EL.But not sure how I can solve this Commented Jan 20, 2010 at 3:35
  • also see stackoverflow.com/questions/1133660/… Commented Jan 20, 2010 at 3:43

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

MessageFormat does not do expressions, though. I suppose you could combine MessageFormat with an expression language, like JUEL.
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.
1

You can use Java's String Templates feature. It is described in JEP 430, and it appears in JDK 21 as a preview feature. Here is an example use:

String outputString = STR."<a href='http://www.ncbi.nlm.nih.gov/pubmed?term=\{someframeworkobject.geneid}' />";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.