morgancodes
morgancodes

Reputation: 25285

getting boolean properties from objects in jsp el

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

Upvotes: 9

Views: 11555

Answers (5)

Eddie
Eddie

Reputation: 54431

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.

Upvotes: 0

kghastie
kghastie

Reputation: 747

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

private boolean isAdmin;

public boolean isAdmin() {
    return isAdmin;
}

public void setAdmin(boolean admin) {
    isAdmin = admin;
}

Upvotes: 0

morgancodes
morgancodes

Reputation: 25285

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)

Upvotes: 4

Andreas Petersson
Andreas Petersson

Reputation: 16518

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.

Upvotes: 0

Eric Wendelin
Eric Wendelin

Reputation: 44399

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.

Upvotes: 0

Related Questions