Java 如何在jstl中使用三元运算符编写if else条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19509561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write if else condition using ternary operator in jstl
提问by user2142786
Hi guys i want to write a if else condition using ternary in JSTL. i did it using jsp. My code is using jsp :
大家好,我想在 JSTL 中使用三元编写 if else 条件。我是用jsp做的。我的代码正在使用 jsp :
<%= relAttributeValue != "false" && !relAttributeValue.equals("false") ? "rel=\"nofollow\"" : "" %>
how can i achieve it using jstl
我如何使用 jstl 实现它
采纳答案by Luiggi Mendoza
You mean Expression Language, EL in short, since that's the component that allows you use ${something}
expressions, while JSTL is a tag library which gives you tag components like <c:set>
.
您的意思是Expression Language,简称 EL ,因为这是允许您使用${something}
表达式的组件,而 JSTL 是一个标记库,可为您提供诸如<c:set>
.
In EL, you can do it like this:
在 EL 中,您可以这样做:
<c:set var="ternaryResult"
value="${(relAttributeValue != 'false') ? 'rel=\"nofollow\"' : ''}" />
Note that in EL you don't need to worry about comparing references using ==
like in Java. More info on this: Is there an equivalent of '==' from Java in EE 6 JSF EL
请注意,在 EL 中,您无需担心==
在 Java 中使用like来比较引用。关于此的更多信息:在 EE 6 JSF EL 中是否有 Java 中的“==”等价物