Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在freemarker中使用三元运算符?_Java_Freemarker - Fatal编程技术网

Java 在freemarker中使用三元运算符?

Java 在freemarker中使用三元运算符?,java,freemarker,Java,Freemarker,我只想做这样的事情: <a href="${ a? 'a.htm' : 'b.htm'}"> <!-- `else` is optional --> ${if(someBoolean, "someBoolean is true")} <!-- expressions --> ${if(someBoolean||otherBoolean, "hello,"+user.name, 1+2+3)} <!-- with parameter names

我只想做这样的事情:

<a href="${ a? 'a.htm' : 'b.htm'}">
<!-- `else` is optional -->
${if(someBoolean, "someBoolean is true")}  

<!-- expressions -->
${if(someBoolean||otherBoolean, "hello,"+user.name, 1+2+3)} 

<!-- with parameter names: not possible with functions,
     but also not really helpful -->

<!-- first in list? -->
<#list seq as x>
    ${if(x_index==0, "first", "not first")}
<#list>

如果您使用的是freemarker 2.3.23或更高版本,您可以使用内置的:

<a href="${a?then('a.htm','b.html')}" target="${openTarget}">
<a href="${a?string('a.htm','b.html')}" target="${openTarget}">
当应用于布尔值时,
字符串
内置将充当三元运算符



此宏提供了一种更直接的方法来执行三元操作:

<#macro if if then else=""><#if if>${then}<#else>${else}</#if></#macro>

由于某些原因,如果无名参数是非布尔表达式,则不能在其周围添加括号。这可能会进一步提高可读性。

如果声明如下,则可以定义自定义函数

<#function if cond then else="">
  <#if cond>
    <#return then>
  <#else>
    <#return else>
  </#if>
</#function>
与@kapep相反,我认为应该使用函数,而不是宏。 宏生成(文本)输出,而函数返回一个值,该值可以指定给变量,但也可以写入输出,因此使用函数更灵活。此外,应用函数的方法更接近于使用三元运算符,它也可以在
${…}
表达式中使用,而不是作为指令

例如,如果多次需要条件链接目标,将其指定给局部变量是有意义的:

<#assign targetUrl=if(a, 'a.htm', 'b.htm')/>
<a href="${targetUrl}">link 1</a>
...
<a href="${targetUrl}">link 2</a>

...
使用函数而不是宏,@kapep的示例如下所示:

<a href="${ a? 'a.htm' : 'b.htm'}">
<!-- `else` is optional -->
${if(someBoolean, "someBoolean is true")}  

<!-- expressions -->
${if(someBoolean||otherBoolean, "hello,"+user.name, 1+2+3)} 

<!-- with parameter names: not possible with functions,
     but also not really helpful -->

<!-- first in list? -->
<#list seq as x>
    ${if(x_index==0, "first", "not first")}
<#list>

${if(someBoolean,“someBoolean为真”)}
${if(someBoolean | | otherBoolean,“hello,”+user.name,1+2+3)}
${if(x_index==0,“第一”,“非第一”)}

从FreeMarker 2.3.23开始,您可以编写
a?,然后('a.htm','b.htm')
condition-then(whenTrue,whenFalse)
优于
condition-string(whenTrue,whenFalse)
的优点是它适用于非字符串
whenTrue
whenFalse
表达式,并且它只计算
whenrue
whenFalse
表达式中的一个(无论选择哪个分支).

使用插值语法:

"${(a?has_content)?string('a.htm','b.htm')}"

has_content:可用于处理字符串(如果字符串为空,则返回FALSE)

Try var url=(a?a.htm:b.htm)。。。。。。。。。。。。。。正如obourgain在下面所说的,您可以使用
?string
,但是
a
的值到底是多少?您想测试它是否存在,或者它是否是布尔值
true
?乍一看,这并不是很明显。我对答案投了赞成票,但老实说,只做一个
可能更可读,而且
不太可读,因为这不是它的预期用途。它用于格式化布尔值,如
registed:${registed?string('yes','no')}
。从2.3.23开始,有
条件?然后(whenTrue,whenFalse)
用于此。@ddekany感谢您提供的信息,我更新了答案以包含新的解决方案。
"${(a?has_content)?string('a.htm','b.htm')}"