Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL if tag for equal strings

I've got a variable from an object on my JSP page:

<%= ansokanInfo.getPSystem() %>

The value of the variable is NAT which is correct and I want to apply certain page elements for this value. How do I use a tag to know the case? I tried something like

<c:if test = "${ansokanInfo.getPSystem() == 'NAT'}">      
   process  
</c:if> 

But the above doesn't display anything. How should I do it? Or can I just as well use scriptlets i.e.

<% if (ansokanInfo.getPSystem().equals("NAT"){ %>
process
<% } %>

Thanks for any answer or comment.

like image 921
Niklas Rosencrantz Avatar asked Apr 17 '12 11:04

Niklas Rosencrantz


People also ask

How check string contains in JSP?

The fn:contains() function determines whether an input string contains a specified substring.


3 Answers

Try:

<c:if test = "${ansokanInfo.PSystem == 'NAT'}"> 

JSP/Servlet 2.4 (I think that's the version number) doesn't support method calls in EL and only support properties. The latest servlet containers do support method calls (ie Tomcat 7).

like image 184
Adam Gent Avatar answered Sep 22 '22 14:09

Adam Gent


<c:if test="${ansokanInfo.pSystem eq 'NAT'}"> 
like image 24
Phani Avatar answered Sep 25 '22 14:09

Phani


I think the other answers miss one important detail regarding the property name to use in the EL expression. The rules for converting from the method names to property names are specified in 'Introspector.decpitalize` which is part of the java bean standard:

This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone.

Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".

So in your case the JSTL code should look like the following, note the capital 'P':

<c:if test = "${ansokanInfo.PSystem == 'NAT'}">
like image 37
Jörn Horstmann Avatar answered Sep 23 '22 14:09

Jörn Horstmann