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?
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.
There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.
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);
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.
Try this :
<c:if test="${user.comLanguage=='ger'}">
Also you can try ternary if:
${user.comLanguage=='ger' ? 'checked' : ''}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With