Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity Eventhandler

Tags:

velocity

in velocity, when you do $object.variable if it not be able to find the getter function to access it or the getter returns a null. it will just show $object.variable explicitly on the page

I know there is a quiet reference, but I don't want to add ! sign to thousands of variables.

I have tried InvalidReferenceEventHandler, NullValueHandler they all didn't get called.

I wander is there a specific type of Eventhandler for this.

Many thanks

like image 800
Junchen Liu Avatar asked May 21 '26 23:05

Junchen Liu


1 Answers

I'm basing this off of Engine-1.7 code.

It seems that when an invalid method is called that the utility method EventHandlerUtil.invalidGetMethod is called. This method creates a new InvalidGetMethodExecutor (this is an inner class on InvalidReferenceEventHandler). Eventually this chains down into a call to invalidReferenceHandlerCall which eventually iterates over any handlerIterators which have been defined. Unfortunately I don't know enough about the internals of Velocity to tell you how to inject these values though. My guess is that the user list will suggest a way to override this behavior or a suggestion will be to use / implement a custom tool.

Edit:

According to the Developer Guide you can do the following. You'll need to write some code to deal with it, but it shouldn't be too difficult:

Pluggable Introspection

runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectImpl

This property sets the 'Uberspector', the introspection package that handles all introspection strategies for Velocity. You can specify a comma-separated list of Uberspector classes, in which case all Uberspectors are chained. The default chaining behaviour is to return the first non-null value for each introspection call among all provided uberspectors. You can modify this behaviour (for instance to restrict access to some methods) by subclassing org.apache.velocity.util.introspection.AbstractChainableUberspector (or implementing directly org.apache.velocity.util.introspection.ChainableUberspector). This allows you to create more interesting rules or patterns for Uberspection, rather than just returning the first non-null value.

like image 97
Scott Avatar answered May 26 '26 02:05

Scott