如果在jstl中使用三元运算符写其他条件,该如何写? [英] How to write if else condition using ternary operator in jstl?

查看:91
本文介绍了如果在jstl中使用三元运算符写其他条件,该如何写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JSTL中使用三进制编写if if条件.我是用jsp完成的.

I want to write a if else condition using ternary in JSTL. I did it using jsp.

我的代码正在使用jsp:

My code is using jsp:

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

如何使用jstl实现它?

How can I achieve it using jstl?

推荐答案

您的意思是表达语言,简称EL,因为那是允许您使用${something}表达式的组件,而JSTL是一个标签库,可为您提供诸如<c:set>的标签组件.

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

在EL中,您可以这样做:

In EL, you can do it like this:

<c:set var="ternaryResult"
    value="${(relAttributeValue != 'false') ? 'rel=\"nofollow\"' : ''}" />

请注意,在EL中,您无需担心像Java中那样使用==比较引用.有关更多信息,请参见:在EE 6 JSF EL中是否存在Java中等效的'=='

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

这篇关于如果在jstl中使用三元运算符写其他条件,该如何写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆