Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing strings in EL [duplicate]

I'm giving a User object to JSP and want to compare an attribute of the user with a given String. What I'm doing right now is the following:

<input type="radio" name="lang" value="ger" <c:if test="${user.comLanguage.equals("ger")}">checked="yes"</c:if>/>German</br>

But all I get is the following Exception:

org.apache.jasper.JasperException: /WEB-INF/jsp/library/home.jsp (line: 22, column: 95) equal symbol expected

where column 95 is one of the letters of comLanguage.

What's the correct syntax here?

like image 943
tschaei Avatar asked Dec 02 '11 11:12

tschaei


People also ask

Can you use == when comparing strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

What are the 3 ways to compare two string objects?

There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.

How do you compare two strings of different lengths?

strcmp is used to compare two different C strings. When the strings passed to strcmp contains exactly same characters in every index and have exactly same length, it returns 0. For example, i will be 0 in the following code: char str1[] = "Look Here"; char str2[] = "Look Here"; int i = strcmp(str1, str2);

How do you compare two strings with the same?

The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.


1 Answers

Try this :

<c:if test="${user.comLanguage=='ger'}">

Also you can try ternary if:

${user.comLanguage=='ger' ? 'checked' : ''}
like image 133
StKiller Avatar answered Oct 14 '22 05:10

StKiller