views:

517

answers:

3

Hello,

I have a Struts + Velocity structure like for example, a Person class, whose one property is a Car object (with its own getter/setter methods) and it is mapped to a Velocity form that submits to an Action, using ModelDriven and getModel structure.

I what to put a button on the form that shows "View Car" if car property is not null or car.id != 0 or show another button "Choose Car" if car is null or car.id = 0.

How do I code this. I tried something like that in the template file:

#if($car != null)
  #ssubmit("name=view" "value=View Car")
#else
  #ssubmit("name=new" "value=Choose Car")
#end

But I keep getting error about Null value in the #if line.

I also created a boolean method hasCar() in Person to try, but I can't access it and I don't know why.

And Velocity + Struts tutorials are difficult to find or have good information.

Thanks

+4  A: 

You should change the #if line to:

#if($car)
bmatthews68
+1  A: 

In the upcoming Velocity 1.6 release, you will be able to do #if( $car == $null ) without error messages. This will allow you to distinguish easily between when $car is null and when it is false. To do that now requires #if( $car && $car != false ), which just isn't as friendly.

Nathan Bubna
Hey Nathan good to see you over here, I've used the Velocity mailing list extensively and always found your help to be spot on. I'm doing my part by voting up your answers, keep it up!
Pete
A: 

Hi Nathan,

I came to know about null check using $null in velocity 1.6 through a resource updated by you. Resource: http://stackoverflow.com/questions/24495/reading-model-objects-mapped-in-velocity-templates But I am facing so many challenges that there is no $null for null check in velocity as there is no documentation provided about this. Please provide me with documentation stating $null as valid for null check in velocity.

Thanks in Advance lucky

lucky