非 JSP(独立)上下文中的 JSTL/JSP EL(表达式语言) [英] JSTL/JSP EL (Expression Language) in a non JSP (standalone) context

查看:28
本文介绍了非 JSP(独立)上下文中的 JSTL/JSP EL(表达式语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以推荐一个框架,用于在独立应用程序中按照 JSP EL(表达式语言)进行模板/格式化消息吗?

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}

为它提供一个上下文,其中将包含一个参数对象的值字典(在这种情况下,一个名为customer"的 Customer 类型的对象,以及一个名为order"的 Order 类型的对象).

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

我知道有很多模板框架 - 其中许多在 Web 应用程序上下文之外工作,但我不认为这是一个大型的重量级模板框架.只是 Java 已经提供的基本消息格式功能的更好版本

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

例如,我可以通过使用模板(或他们称之为模式")来使用 java.text.MessageFormat 完成上述操作,例如

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}

并且我可以在我调用的 Java 程序中向它传递一个 Object 数组

and I can pass it an Object array, in my calling Java program

new Object[] { customer.getFirstName(), order.getEstimatedDispatchDate() };

然而,在这种用法中,代码和模式是紧密相连的.虽然我可以将模式放在资源属性文件中,但代码和模式需要了解彼此的亲密细节.使用类似 EL 的系统,代码和模式之间的契约将处于更高级别(例如客户和订单,而不是 customer.firstName 和 order.estimatedDispatchDate),从而更容易更改结构、订单和内容不改变任何代码的消息.

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.

推荐答案

您可以只使用通用表达式语言本身.您需要一个实现(但有一些可供选择).之后,您需要实现三个类:ELResolver、FunctionMapper 和 VariableMapper.

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.

这篇博文描述了如何做:Java: 在 J2EE 之外使用 EL.

This blog post describes how to do it: Java: using EL outside J2EE.

这篇关于非 JSP(独立)上下文中的 JSTL/JSP EL(表达式语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆