读取Velocity模板中映射的模型对象

时间:2022-11-24 18:43:19

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.

我有一个Struts + Velocity结构,比如一个Person类,它的一个属性是Car对象(有自己的getter / setter方法),它使用ModelDriven和getModel结构映射到提交给Action的Velocity表单。

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.

如果car属性不为null或car.id!= 0,则在显示“View Car”的表单上放置一个按钮,如果car为null或car.id = 0,则显示另一个按钮“Choose Car”。

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.

但是我在#if行中一直收到有关Null值的错误。

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

我还在Person中创建了一个布尔方法hasCar()来尝试,但我无法访问它,我不知道为什么。

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

而Velocity + Struts教程很难找到或有很好的信息。

Thanks

2 个解决方案

#1


6  

You should change the #if line to:

您应该将#if行更改为:

#if($car)

#2


2  

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.

在即将发布的Velocity 1.6版本中,您将能够执行#if($ car == $ null)而不会出现错误消息。这样您就可以轻松区分$ car为空时何为false。要做到这一点,现在需要#if($ car && $ car!= false),这不是那么友好。

#1


6  

You should change the #if line to:

您应该将#if行更改为:

#if($car)

#2


2  

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.

在即将发布的Velocity 1.6版本中,您将能够执行#if($ car == $ null)而不会出现错误消息。这样您就可以轻松区分$ car为空时何为false。要做到这一点,现在需要#if($ car && $ car!= false),这不是那么友好。