有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java有时不能代替velocity

有时,下面的代码在模板中生成$position.getComment()。其他字段也一样。但这只是偶尔的行为。原因可能是什么?如何修复

#if($position.hasComment())
    <td>$position.getComment()</td>
#else
    <td class="empty">&mdash;</td>
#end

Position的注释是字符串,下面是hasComment

public boolean hasComment() {
    return comment != null;
}

在Velocity启动时,使用一些日志可能很有用

velocity - Velocimacro : allowInline = true : VMs can be defined inline in templates
velocity - Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions
velocity - Velocimacro : allowInlineLocal = false : VMs defined inline will be global in scope if allowed.

Velocity templates not substituting methods


共 (1) 个答案

  1. # 1 楼答案

    尝试这样做:

    #if (! $position.getComment() || "$position.getComment()" == "")
        <td class="empty">&mdash;</td>
    #else
        <td>$position.getComment()</td>
    #end
    

    我是velocity新手,所以我无法完全解释原因,但我已经为一个新项目编写了两周的HTML模板,在我的团队中,我们发现这种方法是最好的方法