JSP EL字符串串联 [英] JSP EL String concatenation

查看:94
本文介绍了JSP EL字符串串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在EL中连接字符串?

How do I concatenate strings in EL?

我想做这样的事情,但是不起作用:

I want to do something like this but it doesn't work:

${var1 == 0 ? 'hi' : 'hello ' + var2}

它抛出异常,试图将'hello'强制转换为Double

It throws an exception trying to cast 'hello' to a Double

推荐答案

+运算符始终表示JSP中的数字加法 表达语言 .要进行字符串连接,您必须使用多个相邻的EL表达式,例如${str1}${str2}.

The + operator always means numerical addition in JSP Expression Language. To do string concatenation you would have to use multiple adjacent EL expressions like ${str1}${str2}.

如果我正确阅读了您的示例,则可以写为:

If I read your example correctly this could be written as:

${var1 == 0 ? 'hi' : 'hello '}${var1 == 0 ? '' : var2}


编辑

另一种可能性是使用 JSTL ,它更长,但如果有更多文本依赖于此,则可能会更清晰var1:


Edit

Another possibility would be to use JSTL, which is longer but might be clearer if there is more text that depends on var1:

<c:choose>
    <c:when test="${var1 == 0}">hi</c:when>
    <c:otherwise>hello <c:out value="${var2}"/></c:otherwise>
</c:choose>

根据JSP版本,可能不需要c:out.

The c:out might not be needed, depending on the JSP version.

这篇关于JSP EL字符串串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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