Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

java - Calling class methods (static) from inside a velocity view page

Can you call class methods from inside a view page?

Specifically ones that are not passed into the view?

In asp.net MVC I can do this:

<%= SomeClass.FixDateFormat(ViewData.Model.SomeClass.DateCreated) %>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(found this here: http://velocity.10973.n7.nabble.com/Use-of-static-functions-td15126.html)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...