9

I have an instance of the following object in the jsp page context:

Class User{
  private boolean isAdmin;
  public boolean isAdmin(){return isAdmin}
}

How can I query the isAdmin property from the EL? This doesn't seem to work:

${user.admin}

nor does this:

${user.isAdmin}

thanks!

-Morgan

morgancodes
  • 25,055
  • 38
  • 135
  • 187

5 Answers5

4

Ok. I'm stupid. Vote this question down, ridicule me, etc. The problem was in the method that isAdmin() was delegating to. There was a null pointer exception in that method. In my defense, however, I'll say that the stack trace I got was a bit unclear, and made it look like it was an EL issue rather than a simple null pointer in my code.

Vinegar, your assurances that isAdmin() works even without a property did help me figure this out. Thanks for that.

javax.el.ELException: java.lang.NullPointerException
        at javax.el.BeanELResolver.getValue(BeanELResolver.java:298)
        at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
        at com.sun.el.parser.AstValue.getValue(AstValue.java:138)
        at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:206)
        at org.apache.jasper.runtime.PageContextImpl.evaluateExpression(PageContextImpl.java:1001)
        at org.apache.jsp.WEB_002dINF.jsp.managepermissions_jsp._jspx_meth_c_forEach_1(org.apache.jsp.WEB_002dINF.jsp.managepermissions_jsp:452)
        at org.apache.jsp.WEB_002dINF.jsp.managepermissions_jsp._jspx_meth_c_forEach_0(org.apache.jsp.WEB_002dINF.jsp.managepermissions_jsp:399)
        at org.apache.jsp.WEB_002dINF.jsp.managepermissions_jsp._jspx_meth_form_form_0(org.apache.jsp.WEB_002dINF.jsp.managepermissions_jsp:348)
        at org.apache.jsp.WEB_002dINF.jsp.managepermissions_jsp._jspService(org.apache.jsp.WEB_002dINF.jsp.managepermissions_jsp:197)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:486)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:380)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502)
        at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:363)
        at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
        at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
        at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
        at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417)
        at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:334)
        at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
        at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:240)
        at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:252)
        at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1173)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:901)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:523)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:463)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
morgancodes
  • 25,055
  • 38
  • 135
  • 187
  • 1
    I'm surprised that *none* of us asked for your failure message, which would have told us to look at your code and look for a stack dump. – Eddie Feb 09 '09 at 17:47
  • It's funny but I also had a problem in the object method and thought it's jsp/boolean related... – Moshe Shaham Oct 16 '14 at 12:02
0

Here's how Intellij (and I) would do it:

private boolean isAdmin;

public boolean isAdmin() {
    return isAdmin;
}

public void setAdmin(boolean admin) {
    isAdmin = admin;
}
kghastie
  • 739
  • 1
  • 7
  • 14
0

Try this:

${user.Admin}

just in case capitalization is the problem. Sometimes EL does non-obvious things. However, I've usually been able to just use the equivalent of ${user.admin} in my el. Looking at my own code, I have many examples of doing the obvious thing where it works.

Do you have the following methods in your class:

  public boolean isAdmin(){return isAdmin}

  public void isAdmin(boolean newValue) { ... }

or do you have only the getter? If my code, I notice that I do not do the above. My setters all start with set such as:

  public boolean isAdmin(){return isAdmin}

  public void setAdmin(boolean newValue) { ... }

and I am able to use the obvious lowercase solution ${user.admin} in my JSPs. This may depend on which EL processor you're using.

NOTE: Added later because people still vote this down, obviously never having run into an example where this occurs. An example from my own JSPs that caused me to ask this question is that I have a method:

public int getLANSomething() { ... }

and I access this in EL as follows: ${agent.LANSomething} The rule appears to be getXXXyyy where XXX is all caps, you have to use caps to access it in EL. At least with Tomcat versions 4-6 that I have used.

Eddie
  • 53,828
  • 22
  • 125
  • 145
  • 1
    ${user.Admin} is making no sense here, at all. – Adeel Ansari Feb 06 '09 at 03:12
  • @Vinegar: You have apparently never seen EL want a capital letter as the first letter of a property. I have. I didn't think that that was the answer but I thought it was worth asking. – Eddie Feb 06 '09 at 03:53
  • Revoked my negative. No, I have never. But in which case EL want a capital letter as first letter of a property is the question? – Adeel Ansari Feb 06 '09 at 04:20
  • 1
    @Vinegar: Looking at my JSPs, here is an example. I have a method: public int getLANSomething() { ... } and I access this in EL as follows: ${agent.LANSomething} The rule appears to be getXXXyyy where XXX is *all caps*, you have to use caps to access it in EL. At least with Tomcat 4-6. – Eddie Feb 09 '09 at 17:46
0

First, you probably need a getter for the User class. If that doesn't help, {user.admin} should work, so I'd check that you have the bean properly referenced in your JSP.

Hope that helps.

Eric Wendelin
  • 43,147
  • 9
  • 68
  • 92
0

simple.

for me, just changing isStuff to getStuff worked always.

of course, that may be against some naming convention, declaration of independence, human rights, etc.. but it workds for me.

Andreas Petersson
  • 16,248
  • 11
  • 59
  • 91