93

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.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 1
    Can you please elaborate `getPSystem()` and `ansokanInfo` ? do you set them in the request or accessing them through `import` ? – Santhosh Oct 29 '14 at 05:32
  • 1
    IIRC I was setting them in the request and the solution (workaround) was to upgrade websphere as told by BalusC and then a newer version can invoke bean methods by tags. I was using standard websphere in the question and some version before jsp and jstl could run methods. Please find [my other question](http://stackoverflow.com/questions/10742174/el-equivalent-of-object-methodparameter-in-was-8) where BalusC tells us that I was using a websphere version that needed a newer version to be able to invoke the methods. – Niklas Rosencrantz Oct 29 '14 at 10:10
  • 1
    So now have you upgraded your app server to was 8 ? and does that solution fail ? – Santhosh Oct 29 '14 at 10:18
  • @SanKrish I don't use JSP now. I hope it works for you and that it was updated for newer versions of the app server. – Niklas Rosencrantz Feb 23 '15 at 08:03

4 Answers4

156

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

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • 1
    I updated my answer based on Jorn's answer. Tip of the hat to him. – Adam Gent Apr 17 '12 at 11:57
  • 3
    I have given up on JSP(X). I either use Mustache (https://github.com/samskivert/jmustache) or my own templating language JATL (http://code.google.com/p/jatl/). – Adam Gent Apr 18 '12 at 12:12
  • 1
    How would you do multiple strings? There's got to be a better way than `` – Dillon Dec 02 '15 at 21:04
39
<c:if test="${ansokanInfo.pSystem eq 'NAT'}">
Phani
  • 5,319
  • 6
  • 35
  • 43
16

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'}">
Community
  • 1
  • 1
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • 2
    Getters and setters with a naming convention are pretty much the definition of a java bean. I guess we need some more information regarding where the variable is passed to the jsp. In order to be available to EL it has to be in some scope, for example as a request or pageContext attribute. A local variable created by a script block would not be available to EL. – Jörn Horstmann Apr 18 '12 at 08:45
5

You can use scriptlets, however, this is not the way to go. Nowdays inline scriplets or JAVA code in your JSP files is considered a bad habit.

You should read up on JSTL a bit more. If the ansokanInfo object is in your request or session scope, printing the object (toString() method) like this: ${ansokanInfo} can give you some base information. ${ansokanInfo.pSystem} should call the object getter method. If this all works, you can use this:

<c:if test="${ ansokanInfo.pSystem  == 'NAT'}"> tataa </c:if>
Bombe
  • 81,643
  • 20
  • 123
  • 127
JohanB
  • 2,068
  • 1
  • 15
  • 15