Freemarker - 检查布尔值

Sam*_*and 3 java boolean freemarker

检查 FreeMarker 中表单数据中 boolean 值是否正确的语法是什么,我的代码:

<#if "${form.allStores}" !false>
        <@displayRow label="Receiving Stores" value="All Stores" />
    <#elseif "${form.storesReceiving}" == false || "${form.storesReceiving}"?has_content>
        <@displayRow label="Receiving Stores" value="No Stores"/>
    <#else>
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Could not prepare mail; nested exception is freemarker.core._MiscTemplateException: Can't convert boolean to string automatically, because the "boolean_format" setting was "true,false", which is the legacy default computer-language format, and hence isn't accepted. --
Run Code Online (Sandbox Code Playgroud)

use*_*900 7

Freemarker的有那么函数自2.3.23版本:

<@displayRow label="Receiving Stores" value="${form.allStores?then('All Stores', 'No Stores')}"/>
Run Code Online (Sandbox Code Playgroud)

像 booleanExp?then(whenTrue, whenFalse) 一样使用

也类似于java,你可以使用!运算符来否定:

<#if !form.allStores> 
    <@displayRow label="Receiving Stores" value="No Stores"/>
Run Code Online (Sandbox Code Playgroud)

然后布尔值只能是真/假所以不需要elseif

<#else>
    <@displayRow label="Receiving Stores" value="All Stores" />
</#if>
Run Code Online (Sandbox Code Playgroud)

称为逻辑非运算符。用于反转其操作数的逻辑状态。如果条件为真,则逻辑非运算符将变为假。

也更喜欢使用第一个正条件作为:

<#if form.allStores> 
    <@displayRow label="Receiving Stores" value="All Stores" />
<#else>
    <@displayRow label="Receiving Stores" value="No Stores"/>
</#if>
Run Code Online (Sandbox Code Playgroud)