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

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

问题描述

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

我希望能够实例化某种对象,并按照以下内容为其提供模板

Dear ${customer.firstName}. You order will be dispatched on ${order.estimatedDispatchDate}

为其提供一个上下文,该上下文将包含参数对象的值字典(在这种情况下,名称为Customer的对象名为"customer",而类型为Order的对象名为order). /p>

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

例如,我可以通过使用模板(或称其为模式")(例如

)来使用java.text.MessageFormat完成上述操作

Dear {0}. You order will be dispatched on {1,date,EEE dd MMM yyyy}

然后我可以在调用的Java程序中将其传递给Object数组

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

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

解决方案

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

此博客文章描述了如何执行此操作: Java :在J2EE之外使用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}

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.

解决方案

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.

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

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