配置速度以使用 toString 以外的其他内容渲染对象? [英] Configure velocity to render an object with something other than toString?

查看:38
本文介绍了配置速度以使用 toString 以外的其他内容渲染对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 Velocity 配置为使用 toString() 以外的其他方法将对象转换为模板中的字符串?例如,假设我使用一个带有 format() 方法的简单日期类,并且我每次都使用相同的格式.如果我的所有速度代码都如下所示:

Is there a way to configure Velocity to use something other than toString() to convert an object to a string in a template? For example, suppose I'm using a simple date class with a format() method, and I use the same format every time. If all of my velocity code looks like this:

$someDate.format('M-D-yyyy')

是否有一些我可以添加的配置让我说

is there some configuration I could add that would let me just say

$someDate

相反?(假设我不能只编辑日期类并为其提供适当的 toString()).

instead? (Assuming I'm not in a position to just edit the date class and give it an appropriate toString()).

如果有帮助的话,我是在使用 WebWork 构建的 web 应用程序的上下文中执行此操作的.

I'm doing this in the context of a webapp built with WebWork, if that helps.

推荐答案

您还可以创建自己的 ReferenceInsertionEventHandler 来监视您的日期并自动为您设置格式.

You could also create your own ReferenceInsertionEventHandler that watches for your dates and automatically does the formatting for you.

这篇关于配置速度以使用 toString 以外的其他内容渲染对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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