How to write if else condition using ternary operator in jstl

自作多情 提交于 2020-01-02 05:05:32

问题


Hi guys i want to write a if else condition using ternary in JSTL. i did it using jsp. My code is using jsp :

<%= relAttributeValue != "false" && !relAttributeValue.equals("false") ? "rel=\"nofollow\"" : "" %>

how can i achieve it using jstl


回答1:


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>.

In EL, you can do it like this:

<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



来源:https://stackoverflow.com/questions/19509561/how-to-write-if-else-condition-using-ternary-operator-in-jstl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!