从速度视图页面内调用类方法(静态) [英] Calling class methods (static) from inside a velocity view page

查看:95
本文介绍了从速度视图页面内调用类方法(静态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在视图页面内调用类方法吗?

Can you call class methods from inside a view page?

特别是未传递到视图中的那些?

Specifically ones that are not passed into the view?

在asp.net MVC中,我可以这样做:

In asp.net MVC I can do this:

<%= SomeClass.FixDateFormat(ViewData.Model.SomeClass.DateCreated) %>


推荐答案

因为这出现在谷歌搜索的顶部在这个主题上,似乎人们可能希望看到更新的答案,当他们在搜索的顶部得到这个...

Since this came up in the top of my google search on this topic it seems like folks might like to see an updated answer when they get this on the top of their search...

(在此处找到: http://velocity.10973.n7.nabble.com/Use- of-static-functions-td15126.html

在Velocity 1.5或更早版本中,你可以使用:

in Velocity 1.5 or earlier, you can just use:

#set( $String = '' )
#set( $foo = $String.format('%.1f', $dataFedIn) )

因为你总是可以在实例上调用静态方法。 :)

because you can always call static methods on instances. :)

但是,由于有一些静态类,你不能创建实例(例如java.util.Math),我们在1.6中添加了支持
静态类方法sans实例:

however, since there are some static classes of which you cannot create instances (e.g. java.util.Math), we added support in 1.6 for static class methods sans instances:

Java:

context.put("String", String.class);

Velocity:

#set( $foo = $String.format('%.1f', $dataFedIn) ) 

这篇关于从速度视图页面内调用类方法(静态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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