【问题标题】:If Else Construct with Comparison not Working In Velocity TemplateIf Else Construct with comparison not working in Velocity Template
【发布时间】:2020-04-04 10:07:51
【问题描述】:

我有下面的速度模板构造

#if($no_of_entries > 1)          
    <strong>Its True !!</strong>
#else
    <strong>Its False !!</strong>
#end

即使$no_of_entries 大于 1比如说 10, 它也会打印 Its False !!

这意味着$no_of_entries &gt; 1 不工作

为什么 $no_of_entries &gt; 1 条件返回 false ?

我尝试打印$no_of_entries 的值,它打印出正确的值> 1

编辑:

我也尝试过使用下面的代码

#if( Integer.parseInt($no_of_entries) > 1)    

    <strong>Its True !!</strong>
#else

     <strong>Its False !!</strong>
#end

但它不起作用并抛出异常 -

org.apache.velocity.exception.ParseErrorException: Encountered "Integer" at file.vm
Was expecting one of:
    "[" ...
    "{" ...
    "(" ...
    <WHITESPACE> ...
    <STRING_LITERAL> ...
    "true" ...
    "false" ...
    <INTEGER_LITERAL> ...
    <FLOATING_POINT_LITERAL> ...
    <IDENTIFIER> ...
    "{" ...
    <WHITESPACE> ...

【问题讨论】:

  • no_of_entries 对象是什么?列表/整数/...
  • 哦。它的字符串。这就是为什么它似乎不起作用。让我通过 int 并检查
  • 您使用的是哪个版本的 Velocity?

标签: velocity velocity-template-language


【解决方案1】:

如果是数字应该可以,如果是字符串,parse it as Integer:

#set($Integer = 0)
#if($Integer.parseInt($no_of_entries) > 1) 

或者如果是列表使用size()方法

#if($no_of_entries.size() > 1) 

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-05-24
  • 2014-11-21
  • 2022-12-28
  • 2018-09-11
  • 1970-01-01
  • 2013-11-26
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多