Last updated : 01/10/2002; 18:20:46

Joe's Jelly

Joe Walnes rambling on and on about Software Development, Java and Extreme Programming.

NAVIGATION >>

/WIKI /WEBLOG /THOUGHTWORKS /OPENSYMPHONY /EMAIL

:: ELSEWHERE
xp developer wiki
c2 xp wiki
the server side
javaworld
sd magazine
jakarta

:: BLOGS
Mike Cannon-Brookes
Graham Glass
Paul Hammant
Elliotte Rusty Harold
Darren Hobbs
Patrick Lightbody
Charles Miller
Brett Morgan
Rickard Öberg
Mike Roberts
Sam Ruby
Joel Spolsky
James Strachan
Clemens Vasters

:: INVOLVEMENTS
SiteMesh
QDox
MockDoclet
OSCore
OSUser
PropertySet
RichTags
Marathon
SiteMesh C++
Alt-RMI
Jelly MockTags
more...
:: BACKLOGS
September 2002
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          
Aug   Oct



Click here to visit the Radio UserLand website.

:. 05 September 2002

  8:02:22 AM  

Why Velocity Does It That Way. Jeff Duska wrote a rant on Velocity:

This let talk about a rant of mine with Velocity. From my work with Velocity, it was acting like the it was a bad reference. The default feature of Velocity is to show the Velocity script when the object your are using is null/invalid in the current context. I think it should think never show the end-user my code, I would expect nothing to display or something like the word Error!!! to display.

If Velocity worked that way then every $ which should be printed would have to be escaped. I think that Velocity is doing it the right way. Right now you just have to start your references with $! instead of $ and bang it won't show up if there is no object for that reference.

[All Things Java]

Velocity Tip

You can override the behaviour of how values are displayed on the page by creating a ReferenceInsertionEventHandler and attaching it to an EventCartridge which is then bound to your Velocity Context.

I use this a lot so the default behaviour for $blah is the same as $!blah (one less char for my lazy brain to remember). This enables something else also... you can allow all $blah values to automatically have their HTML entities escaped without having to think about it - nice eh?

Here's how I override this in WebWorkVelocityServlet:

public class HTMLWebWorkVelocityServlet extends WebWorkVelocityServlet {
  protected Context createContext( HttpServletRequest request, HttpServletResponse response ) {
    Context ctx = super.createContext( request, response );
    // setup event handler to escape values as they are inserted into page.
    EventCartridge cartridge = new EventCartridge();
    cartridge.addEventHandler( new ReferenceInsertionEventHandler() {
      public Object referenceInsert( final String reference, final Object value ) {
        if ( value == null || value.toString().length() == 0 ) {
          // if no value, return empty string instead of null.
          return "";
        }
        else {
          // if value present, escape HTML unfriendly chars.
          return TextUtils.htmlEncode( value.toString() );
        }
      }
    } );
    cartridge.attachToContext( ctx );
    return ctx;
  }
}

(For a quick way to encode HTML chars, see TextUtils.htmlEncode() in OSCore).


Web-design by John Doe from open source web design.