velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henning P. Schmiedehausen" <>
Subject Re: #set silently fails if RHS is null
Date Mon, 12 Nov 2001 12:34:33 GMT
"Geir Magnusson Jr." <> writes:

>I have abhorred this aspect of things for a long time - I think of it as a
>bug in a sense because what happens is that he #set() 'fails' based on data,
>which is equally as bad as configurable language semantics...

>I.e. Looking at 

>#set($bar = "woogie")
>#set($bar = $this.couldReturnNull() )

>Means I have *no clue* what $bar has.  It could contain the result of the
>couldReturnNull() method, or not, and be unchanged.  That¹s weird for an
>assignment in any language.

But the main problem here is (IMHO) the fact that null values are
treated specially. 

I worked around a complicated (looking) problem with Turbine and
Velocity (I wrote about it a while ago, the Subject was "what is a
velocity reference", on th dev mailinglist) by using a wrapper class
that is guranteed to be never null:

--- cut ---
public class VeloWrapper
  Object obj = null;

  public VeloWrapper(Object obj)
    this.obj = obj;

  public Object get()
    return this.obj;

  public void put(Object obj)
    this.obj = obj;
--- cut ---

and now use an Intake Tool like this:

--- cut ---
public class VeloIntakeTool extends IntakeTool
  public Group veloMapper(VeloWrapper v, String name) throws Exception
    Object obj = v.get();
    IntakeTool.PullHelper ph = (IntakeTool.PullHelper)get(name);

    return (obj == null) ? ph.getDefault() : ph.mapTo((Retrievable)obj);
--- cut ---

Now I use the following code to the assured happiness of web template
designers and business logic programmers:

--- cut ---
public class myScreen extends VelocityScreen
  public void doBuildTemplate(RunData data, Context ctx)
    throws Exception
    VeloWrapper v = (VeloWrapper)ctx.get("bl");
    BusinessLogicData bl = null;

    if(v != null)
      bl = (BusinessLogicData)v.get();

    ctx.put("bl", new VeloWrapper(bl));
--- cut ---


--- cut ---
#set ($frm = $intake.veloMapper($bl, "businessLogicForm"))

[... the form itself ...]
--- cut --

If I don't have an object, I get the default values for my form (from
intake). If I have an object, I get the values of the object mapped by
intake for my form.

_One_ line of code in the tempate itself. I never asked for more. ;-)
If Velocity would be able to work with null values for keys, I
wouldn't need the wrapper classes above. I consider these "jumping
through unneccessary loops". :-)


Dipl.-Inf. (Univ.) Henning P. Schmiedehausen       -- Geschaeftsfuehrer
INTERMETA - Gesellschaft fuer Mehrwertdienste mbH

Am Schwabachgrund 22  Fon.: 09131 / 50654-0
D-91054 Buckenhof     Fax.: 09131 / 50654-20   

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message