Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two object variables in EL expression language?

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

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.

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

The J2EE 1.4 Tutorial

like image 928
Caroline Avatar asked Dec 14 '09 13:12

Caroline


People also ask

What are the types of expression in El?

JSP EL allows you to create expressions both (a) arithmetic and (b) logical. Within a JSP EL expression, you can use integers, floating point numbers, strings, the built-in constants true and false for boolean values, and null.

Is El ignored?

The valid values of this attribute are true and false . If it is true , EL expressions are ignored when they appear in static text or tag attributes. If it is false , EL expressions are evaluated by the container. The default value varies depending on the version of the Web application deployment descriptor.

How we can use EL in JSP?

Expression Language (EL) in JSP The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean component, and other objects like request, session, application etc. There are many implicit objects, operators and reserve words in EL. It is the newly added feature in JSP technology version 2.0.

What is the main purpose of expression language?

Expression language (EL) has been introduced in JSP 2.0. The main purpose of it to simplify the process of accessing data from bean properties and from implicit objects. EL includes arithmetic, relational and logical operators too.


2 Answers

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

And you need to make sure that the entire expression is placed inside the same ${...} scope. Anything outside that is not interpreted as part of an EL expression.

So the particular line

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

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

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

or, equivalently

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

Both are behind the scenes roughly interpreted as

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

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

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

or, equivalently

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

which is behind the scenes roughly interpreted as

jspContext.findAttribute("lang").equals("en")
like image 93
BalusC Avatar answered Sep 27 '22 16:09

BalusC


Not sure if I get you right, but the simplest way would be something like:

<c:if test="${languageBean.locale == 'en'">
  <f:selectItems value="#{customerBean.selectableCommands_limited_en}" />
</c:if>

Just a quick copy and paste from an app of mine...

HTH

like image 30
KB22 Avatar answered Sep 27 '22 17:09

KB22