简体   繁体   中英

How to write if else condition using ternary operator in jstl?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM