2

问候,

在我正在开发的 web 应用程序中,我想做如下的事情:

我有一个像

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

所以 outputString 被插值如下:http://www.ncbi.nlm.nih.gov/pubmed?term=gene19191X

是否有任何轻量级的 EL 框架可供我使用?

4

2 回答 2

3

也许MVEL会为你工作。

使用类似的模板

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

你可以做

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

看看这个教程(我在哪里读到这个,我没有使用 MVEL 的经验)。

于 2010-01-20T04:03:24.037 回答
2

听起来您只需要核心 Java 库类MessageFormat。它非常易于使用,并允许您替换字符串中的模板。

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

您还可以创建 MessageFormat 的实例并使用不同的值重用它。

您还可以尝试的其他选项是:

  • Apache Commons EL - 这是为 Web 应用程序中的表达式而构建的。
  • Groovy GStrings - 我有时使用它来评估“脚本”。这具有允许更复杂逻辑的优点。
于 2010-01-20T03:48:33.643 回答