tags:

views:

1324

answers:

4

Hello,

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

+1  A: 

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.

Eddie
nope. thanks for the idea though.
morgancodes
Check out my recent edit.
Eddie
${user.Admin} is making no sense here, at all.
Adeel Ansari
@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
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
@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
A: 

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
There is a getter present, already.
Adeel Ansari
A: 

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
But it should work with isXxxx(). And its working here with me.
Adeel Ansari
I think it depends on the version of JSP you're running against. Older versions may need getIsXxxx() while newer versions support isXxxx().
wrumsby
An update to my comment isXxxx() works if the return type is boolean, but does not seem to work if the return type is Boolean.
wrumsby
A: 

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
I am glad it worked :)
Adeel Ansari
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