java 从jsp el中的对象获取布尔属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/518354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:43:52  来源:igfitidea点击:

getting boolean properties from objects in jsp el

javajsp

提问by morgancodes

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

我在 jsp 页面上下文中有以下对象的实例:

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:

如何从 EL 查询 isAdmin 属性?这似乎不起作用:

${user.admin}

nor does this:

这也不是:

${user.isAdmin}

thanks!

谢谢!

-Morgan

-摩根

采纳答案by morgancodes

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.

行。我真笨。投票否决这个问题,嘲笑我等等。问题出在 isAdmin() 委托给的方法中。该方法中存在空指针异常。然而,在我的辩护中,我会说我得到的堆栈跟踪有点不清楚,让它看起来像是一个 EL 问题,而不是我代码中的一个简单的空指针。

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

醋,您对 isAdmin() 即使没有属性也能工作的保证确实帮助我解决了这个问题。感谢那。

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)

回答by kghastie

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

以下是 Intellij(和我)将如何做到这一点:

private boolean isAdmin;

public boolean isAdmin() {
    return isAdmin;
}

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

回答by Eddie

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.

以防万一大写是问题。有时 EL 会做一些不明显的事情。但是,我通常能够${user.admin}在我的 el. 看看我自己的代码,我有很多在它起作用的地方做显而易见的事情的例子。

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 setsuch as:

或者你只有吸气剂?如果我的代码,我注意到我没有执行上述操作。我的二传手都以set这样的开头:

  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.

我可以${user.admin}在我的 JSP 中使用明显的小写解决方案。这可能取决于您使用的 EL 处理器。

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:

注意:稍后添加是因为人们仍然对此进行投票,显然从未遇到过发生这种情况的示例。我自己的 JSP 中让我提出这个问题的一个例子是,我有一个方法:

public int getLANSomething() { ... }

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

我访问这个在EL如下:${agent.LANSomething}规则似乎是getXXXyyy在那里XXX是全部大写,你必须使用上限来访问它EL。至少在我使用过的 Tomcat 版本 4-6 中。

回答by Eric Wendelin

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.

首先,您可能需要 User 类的 getter。如果这没有帮助,{user.admin} 应该可以工作,所以我会检查您是否在 JSP 中正确引用了 bean。

Hope that helps.

希望有帮助。

回答by Andreas Petersson

simple.

简单的。

for me, just changing isStuff to getStuff worked always.

对我来说,只是将 isStuff 更改为 getStuff 始终有效。

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

当然,这可能违反某些命名约定、独立宣言、人权等。但它对我有用。