1

I have a Java object User in my servlet, which I assign to the request parameter "user" in my JSP.

This user has a boolean method hasConfidentialAccess(), which returns true or false. I want to call this in my jsp like the following:

<c:if test="${user.hasConfidentialAccess}">
...
</c:if>

But this doesn't work, my console throws following exception:

11:34:49,978 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/watson].[BasicSearchControllerServlet]] (http-/0.0.0.0:8080-7) JBWEB000236: Servlet.service() for servlet BasicSearchControllerServlet threw exception: javax.el.PropertyNotFoundException: The class 'com.commons.framework.security.DefaultUser' does not have the property 'hasConfidentialAccess'.

How to make this work?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jonas
  • 769
  • 1
  • 8
  • 37
  • 1
    Just call it as method... `${user.hasConfidentialAccess()}` (name of the getter does not conform to bean introspection rules). Of course this requires you to be at least at servlet 2.5. – Pavel Horal Mar 03 '16 at 11:03
  • Correction: Servlet 3.0. This approach is however discouraged as it's basically abuse. – BalusC Mar 03 '16 at 11:13
  • Indeed, forgot the brackets. Damn, thanks – Jonas Mar 03 '16 at 11:14

1 Answers1

4

EL does support accessing isX() methods directly as if you were accessing a getX() method, but only if the return type of the isX() method is a primative boolean.

If you return an object of any kind (such as Boolean isObjectBooleanTrue()) then EL fails to find the method and will give you a rather nasty EL exception: javax.el.PropertyNotFoundException: The class 'com.User' does not have the property 'isConfidentialAccess'.

So yes, 'is' methods work in EL but make sure you ONLY return primitive booleans from them.

Specific to your problem:

  1. Change hasConfidentialAccess() to isConfidentialAccess(), as java bean standard only follows is for boolean return types.
  2. Change the return type to boolean primitive (if currently you have Boolean), otherwise its fine.
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Gurpreet Singh
  • 380
  • 4
  • 9