javax.el.PropertyNotFoundException:属性 'foo' 在 java.lang.Boolean 类型上不可读

Posted

技术标签:

【中文标题】javax.el.PropertyNotFoundException:属性 \'foo\' 在 java.lang.Boolean 类型上不可读【英文标题】:javax.el.PropertyNotFoundException: Property 'foo' not readable on type java.lang.Booleanjavax.el.PropertyNotFoundException:属性 'foo' 在 java.lang.Boolean 类型上不可读 【发布时间】:2012-12-06 19:40:21 【问题描述】:

我有一个看起来像这样的类:

public class ScoreDefinition 

    protected Boolean primary;

    public Boolean isPrimary() 
        return primary;
    

    public void setPrimary(Boolean value) 
        this.primary = value;
    


我正在尝试像这样在 EL 中访问其 primary 属性:

<c:forEach var="score" items="$scores">
    <input type="checkbox"
           value="$score.primary"
           name="someName"
           class="textField"/>
</c:forEach>

但我得到了例外:

javax.el.PropertyNotFoundException: Property 'primary' not readable on type java.lang.Boolean
    at javax.el.BeanELResolver$BeanProperty.read(BeanELResolver.java:280)
    at javax.el.BeanELResolver$BeanProperty.access$000(BeanELResolver.java:230)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:81)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:123)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:938)
    at org.apache.jsp.WEB_002dINF.views.administer.projectBlocks_jsp._jspx_meth_c_005fforEach_005f2(projectBlocks_jsp.java:806)
    at org.apache.jsp.WEB_002dINF.views.administer.projectBlocks_jsp._jspx_meth_c_005fif_005f2(projectBlocks_jsp.java:709)
    at org.apache.jsp.WEB_002dINF.views.administer.projectBlocks_jsp._jspx_meth_form_005fform_005f0(projectBlocks_jsp.java:245)
    at org.apache.jsp.WEB_002dINF.views.administer.projectBlocks_jsp._jspService(projectBlocks_jsp.java:150)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.groupgti.webclient.filter.SecurityFilter.doFilter(SecurityFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.groupgti.webclient.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

我无法更改此类,因为它是从 WSDL 文件生成的。所以问题是如何解决这个问题,而不使用任何自定义 EL 函数将 Boolean 转换为 String 或不将此对象转换为我的对象?是否可以在不进行任何其他修改的情况下以某种方式在 EL 中读取此属性?

【问题讨论】:

【参考方案1】:

is 前缀仅适用于 boolean,不适用于 Boolean

你有两个选择:

    使用boolean 代替Boolean

    或者,将方法 isPrimary() 重命名为 getPrimary()

至于自动生成错误方法的JAXB blunder,您需要至少升级到JAXB 2.1.13 或按照JAXB documentation 将-B-enableIntrospection 添加到wsimport 调用中。

另见:

JavaBeans specification - Chapter 8.3.2 How does Java expression language resolve boolean attributes? (in JSF 1.2) JSF 2 checkboxes and boolean getters

【讨论】:

不能重命名,类是自动生成的,改不了。那么这是否意味着我必须围绕自己进行一些工作? 你能添加你自己的方法吗? 我不做 Spring,但如果我猜对了,是 JAXB 做了自动生成,请参阅更新的答案。 是的,这是 JAXB 生成的。好的,我会添加这个。谢谢。【参考方案2】:

您可以像这样直接调用 score.isPrimary() 方法:

<c:forEach var="score" items="$scores">
<input type="checkbox"
       value="$score.isPrimary()"
       name="someName"
       class="textField"/>
</c:forEach>

【讨论】:

以上是关于javax.el.PropertyNotFoundException:属性 'foo' 在 java.lang.Boolean 类型上不可读的主要内容,如果未能解决你的问题,请参考以下文章