简体   繁体   English

配置速度以使用toString以外的其他内容渲染对象?

[英]Configure velocity to render an object with something other than toString?

Is there a way to configure Velocity to use something other than toString() to convert an object to a string in a template? 有没有办法配置Velocity使用toString()以外的东西将对象转换为模板中的字符串? For example, suppose I'm using a simple date class with a format() method, and I use the same format every time. 例如,假设我使用带有format()方法的简单日期类,并且每次都使用相同的格式。 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

instead? 代替? (Assuming I'm not in a position to just edit the date class and give it an appropriate toString()). (假设我无法编辑日期类并给它一个合适的toString())。

I'm doing this in the context of a webapp built with WebWork, if that helps. 我在使用WebWork构建的webapp的上下文中这样做,如果这有帮助的话。

I faced this problem too and I was able to solve it based on Nathan Bubna answer . 我也遇到了这个问题,我能够根据Nathan Bubna的答案解决这个问题

I'm just trying to complete the answer providing the link to Velocity documentation which explains how to use EventHandlers. 我只想完成答案,提供Velocity文档链接,文档解释了如何使用EventHandlers。

In my case, I needed Velocity calls "getAsString" instead of toString method for all JsonPrimitive objects from gson library every time that a reference was inserted. 就我而言,每次插入引用时,我都需要Velocity为gson库中的所有JsonPrimitive对象调用“getAsString”而不是toString方法。

It was as simple as creating a 这就像创建一个简单

public class JsonPrimitiveReferenceInsertionEventHandler implements ReferenceInsertionEventHandler{

    /* (non-Javadoc)
     * @see org.apache.velocity.app.event.ReferenceInsertionEventHandler#referenceInsert(java.lang.String, java.lang.Object)
     */
    @Override
    public Object referenceInsert(String reference, Object value) {
        if (value != null && value instanceof JsonPrimitive){
            return ((JsonPrimitive)value).getAsString();
        }
        return value;
    }

}

And add the event to the VelocityContext 并将事件添加到VelocityContext

vec = new EventCartridge();
vec.addEventHandler(new JsonPrimitiveReferenceInsertionEventHandler());

...

context.attachEventCartridge(vec);

Velocity allows for a JSTL like utility called velocimacros: Velocity允许使用名为velocimacros的JSTL实用工具:

http://velocity.apache.org/engine/devel/user-guide.html#Velocimacros http://velocity.apache.org/engine/devel/user-guide.html#Velocimacros

This would allow you to define a macro like: 这将允许您定义一个宏,如:

#macro( d $date)
   $date.format('M-D-yyyy')
#end

And then call it like so: 然后像这样调用它:

#d($someDate)

您还可以创建自己的ReferenceInsertionEventHandler来监视日期并自动为您进行格式化。

Oh, and the 1.6+ versions of Velocity have a new Renderable interface. 哦,Velocity的1.6+版本有一个新的Renderable接口。 If you don't mind tying your date class to a Velocity API, then implement this interface and Velocity will use the render(context, writer) method (for your case, you just ignore the context and use the writer) instead of toString(). 如果您不介意将日期类与Velocity API绑定,那么实现此接口,Velocity将使用render(context,writer)方法(对于您的情况,您只需忽略上下文并使用writer)而不是toString( )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM