如何比较EL表达式语言中的两个对象变量? [英] How to compare two object variables in EL expression language?

查看:298
本文介绍了如何比较EL表达式语言中的两个对象变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建所有语言的下拉列表.列表的默认语言选择将由用户添加的信息决定:

I am creating a drop down list of all languages. The default language selection for the list will be determined by information added by the user:

<select>
    <c:forEach items="${languages}" var="lang">
        <c:choose>
            <c:when test="${lang}.equals(${pageLang})">
                <option value="${lang}" selected>${lang}</option>
            </c:when>
            <c:otherwise>
                <option value="${lang}">${lang}</option>
            </c:otherwise>
        </c:choose>
    </c:forEach>
</select>

.equals在EL中似乎不存在.看过这里后,建议我编写自己的函数,然后导入并使用它.因为这仅仅是此页面的一件小事,所以我不想为此而开始创建库等.我也不想开始为servlet创建专家对象以返回其中的额外信息.

.equals doesn't appear to exist in EL. Having had a look here it's suggested I write my own function and then import and use that. As this is a one off tiny thing just for this page I don't want to have to start creating libraries etc just for this. Nor do I want to start creating specialist objects for the servlet to return with this extra info in them.

我唯一想做的就是从servlet中返回整个选项行的实际html,而不仅仅是返回语言字符串,但这使我感到很丑陋,所以我希望有一个更优雅的解决方案.

Only thing I can think to do is to return the actual html for the whole option line from the servlet rather than just the language string, but that strikes me as ugly so I'm hoping there's a more elegant solution.

快速比较EL中两个字符串的最佳方案是什么?

What is the best plan for a quick fix to comparing two strings in EL?

J2EE 1.4指南

推荐答案

In Expression Language you can just use the == or eq operator to compare object values. Behind the scenes they will actually use the Object#equals(). This way is done so, because until with the current EL 2.1 version you cannot invoke methods with other signatures than standard getter (and setter) methods (in the upcoming EL 2.2 it would be possible).

所以这行

<c:when test="${lang}.equals(${pageLang})">

应该写为(请注意,整个表达式在{}内部)

should be written as (note that the whole expression is inside the { and })

<c:when test="${lang == pageLang}">

或等效地

<c:when test="${lang eq pageLang}">

两者均在幕后,大致解释为

Both are behind the scenes roughly interpreted as

jspContext.findAttribute("lang").equals(jspContext.findAttribute("pageLang"))

如果要比较恒定的String值,则需要用引号引起来

If you want to compare constant String values, then you need to quote it

<c:when test="${lang == 'en'}">

或等效地

<c:when test="${lang eq 'en'}">

位于幕后的粗略解释为

jspContext.findAttribute("lang").equals("en")

这篇关于如何比较EL表达式语言中的两个对象变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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