非 JSP(独立)上下文中的 JSTL/JSP EL(表达式语言)
谁能推荐一个框架,用于在独立应用程序中按照 JSP EL(表达式语言)进行模板化/格式化消息?
我希望能够实例化某种类型的对象,给它一个模板,为
Dear ${customer.firstName}. You order will be dispatched on ${order.estimatedDispatchDate}
它提供一个上下文,其中包括参数对象的值字典(在本例中是一个名为“Customer”的对象)例如,“customer”,以及一个名为“order”的 Order 类型的对象)。
我知道有很多模板框架 - 其中许多在 Web 应用程序上下文之外工作,但我不认为这是一个大的重量级模板框架。 只是 Java 已经提供的基本消息格式功能的更好版本
例如,我可以通过使用模板(或他们所说的“模式”)使用 java.text.MessageFormat 来完成上述操作,并且
Dear {0}. You order will be dispatched on {1,date,EEE dd MMM yyyy}
我可以将其传递给对象数组,在我调用Java程序中
new Object[] { customer.getFirstName(), order.getEstimatedDispatchDate() };
但是,在这种用法中,代码和模式是紧密相连的。 虽然我可以将模式放入资源属性文件中,但代码和模式需要了解彼此的详细信息。 使用类似 EL 的系统,代码和模式之间的契约将处于更高的级别(例如,客户和订单,而不是 customer.firstName 和 order.estimatedDispatchDate),从而更容易更改结构、订单和内容的消息,无需更改任何代码。
Can anyone recommend a framework for templating/formatting messages in a standalone application along the lines of the JSP EL (Expression Language)?
I would expect to be able to instantiate a an object of some sort, give it a template along the lines of
Dear ${customer.firstName}. You order will be dispatched on ${order.estimatedDispatchDate}
provide it with a context which would include a value dictionary of parameter objects (in this case an object of type Customer with a name 'customer', say, and an object of type Order with a name 'order').
I know there are many template frameworks out there - many of which work outside the web application context, but I do not see this as a big heavyweight templating framework. Just a better version of the basic Message Format functionality Java already provides
For example, I can accomplish the above with java.text.MessageFormat by using a template (or a 'pattern' as they call it) such as
Dear {0}. You order will be dispatched on {1,date,EEE dd MMM yyyy}
and I can pass it an Object array, in my calling Java program
new Object[] { customer.getFirstName(), order.getEstimatedDispatchDate() };
However, in this usage, the code and the pattern are intimately linked. While I could put the pattern in a resource properties file, the code and the pattern need to know intimate details about each other. With an EL-like system, the contract between the code and the pattern would be at a much higher level (e.g. customer and order, rather then customer.firstName and order.estimatedDispatchDate), making it easier to change the structure, order and contents of the message without changing any code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Freemarker 会完全满足您的需求。 这是一个模板引擎,其语法与 JSP 非常相似:
http://freemarker.org/
Freemarker would do exactly what you need. This is a template engine with a syntax very similar to JSP :
http://freemarker.org/
啊啊。 而使用 MessageFormat,我可以
在参数 #1 是 Date 对象的情况下执行操作,并且它根据模式进行格式化,但在 EL 中没有等效项。
在 JSP 中,我可能会使用格式标记。 在此独立示例中,在计算表达式之前,我必须在代码中将日期格式化为字符串。
AAh. Whereas with MessageFormat, I can do
where parameter #1 is a Date object and it gets formatted according to the pattern, there is no equivalent in EL.
In JSP, I would have used, perhaps, a format tag. In this standalone example, I am going to have to format the Date as a String in my code prior to evaluating the expression.
Ed 提倡在 Java EE 之外使用 EL 本身的想法Burns 和在服务器端讨论。 Tomcats 实现 在单独的 JAR 中提供,但我不知道它是否可以使用服务器之外。
The idea of using EL itself outside of Java EE was advocated by Ed Burns and discussed on The Server Side. Tomcats implementation ships in a separate JAR but I don't know if it can be used outside the server.
我会选择 Spring 表达式语言:
http: //docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html
一些演示其威力的示例(前两个来自文档):
表达式也可以使用对象属性:
I would go for the Spring Expression language:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html
A few examples which demonstrate the power (the first two are from the documentation):
Expressions can also use object properties:
您可能想看看 OGNL 这是您想要的那种库。 OGNL 相当强大,是 WebWork Web 框架中使用的表达语言。
You might want to look at OGNL which is the kind of library you are after. OGNL can be reasonably powerful, and is the expression language used in the WebWork web framework.
回复:Jasper 和 Juel 正在为 1.5 构建:然后我发现了 RetroTranslator (http://retrotranslator.sourceforge.net/< /a>)。 一旦逆向翻译,EL 和 Jasper 就会发挥魅力
Re: Jasper and Juel being built for 1.5: And then I discovered RetroTranslator (http://retrotranslator.sourceforge.net/). Once retrotranslated, EL and Jasper works like a charm
您可以像 jsp 一样使用 Casper,并且易于使用:Casper
You can use Casper very similar to jsp and easy to use : Casper
我建议查看 Apache Velocity。 它非常简单且轻便。
我们目前正在将它用于我们的电子邮件模板,并且效果非常好。
I would recommend looking into Apache Velocity. It is quite simple and lightweight.
We are currently using it for our e-mail templates, and it works very well.
StringTemplate 是 Velocity 和 Freemarker 的更轻量级替代品。
StringTemplate is a more lightweight alternative to Velocity and Freemarker.
您可以只使用通用表达语言本身。 您需要一个实现(但有几个可供选择)。 之后,您需要实现三个类:ELResolver、FunctionMapper 和 VariableMapper。
这篇博文描述了如何做到这一点: Java:使用 EL在 J2EE 之外。
You can just use the Universal Expression Language itself. You need an implementation (but there are a few to choose from). After that, you need to implement three classes: ELResolver, FunctionMapper and VariableMapper.
This blog post describes how to do it: Java: using EL outside J2EE.