javax.el.PropertyNotFoundException:在com.example.Bean类型上找不到属性'foo'

时间:2011-12-20 15:12:32

标签: jsp jstl javabeans el propertynotfoundexception

我有

的结果
Query query = session.createQuery("From Pool as p left join fetch p.poolQuestion as s");

查询,我想在JSP上显示它。

我有循环:

<c:forEach items="${pools}" var="pool"> 

    <p>${pool.name}</p>

</c:forEach>

我想显示poolQ​​uestion表(Join表)的结果。我要显示的值是“回答”。

我该怎么做?

<c:forEach items="${pools}" var="pool"> 
    <p>${pool.answer}</p>
    <p>${pool.name}</p>             
</c:forEach>

上述代码不起作用。

错误是:

  org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 21

18:     <c:forEach items="${pools}" var="pool"> 
19:             
20:             <p>${pool.name}</p>
21:             <c:out value="${pool.poolQuestion.answer}"/>
22:             
23:     </c:forEach>
24: 

    SEVERE: Servlet.service() for servlet appServlet threw exception
javax.el.PropertyNotFoundException: Property 'answer' not found on type com.pool.app.domain.Pool
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:214)
    at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:191)
    at javax.el.BeanELResolver.property(BeanELResolver.java:300)
    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.home_jsp._jspx_meth_c_005fforEach_005f0(home_jsp.java:119)
    at org.apache.jsp.WEB_002dINF.views.home_jsp._jspService(home_jsp.java:78)
    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:250)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    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 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    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:291)
    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:680)

模型是:

public List<Pool> getAll(){

    Session session = sessionFactory.getCurrentSession();

    Query query = session.createQuery("From Pool as p left join fetch p.poolQuestions as s");

    return query.list();

控制器是:

List<Pool> pool = poolService.getAll();

model.addAttribute("pools", pool);

视图是:

<c:forEach items="${pools}" var="pool"> 

    <p>${pool.name}</p>
    <c:out value="${pool.answer}"/>

</c:forEach>

PS。 ${pool.name}正确显示

6 个答案:

答案 0 :(得分:64)

  

javax.el.PropertyNotFoundException:在com.example.Bean类型中找不到属性'foo'

这实际上意味着所提到的类com.example.Bean没有针对上述属性foo的公共(非静态!)getter方法。请注意,字段本身与此无关!

公共getter方法名称必须以get开头,后跟属性名称,该名称仅在Foo中的属性名称的第一个字母处大写。

public Foo getFoo() {
    return foo;
}

因此,您需要确保存在与属性名称完全匹配的getter方法,并且该方法为public - static)并且方法不接受任何参数,并返回非void。如果你有一个它仍然无法正常工作,那么你很可能正在忙着编辑代码而不必坚定地清理构建,重建代码并重新部署/重新启动应用程序。你需要确保你已经这样做了。

对于boolean(不是Boolean!)属性,getter方法名称必须以is开头,而不是get

public boolean isFoo() {
    return foo;
}

无论类型如何,foo字段本身的存在因此相关。它可以有不同的名称,也可以完全不存在,甚至可以是static。以下所有内容仍应由${bean.foo}访问。

public Foo getFoo() {
    return bar;
}

public Foo getFoo() {
    return new Foo("foo");
}

public Foo getFoo() {
    return FOO_CONSTANT;
}

你看,这个字段不重要,但getter方法本身。请注意,属性名称本身不应在EL中大写。换句话说,${bean.Foo}将不起作用,它应该是${bean.foo}

另见:

答案 1 :(得分:6)

我认为id访问器与bean命名约定不匹配,这就是抛出异常的原因。它们应该如下:

public Integer getId() { return id; }    
public void setId(Integer i){ id= i; }

答案 2 :(得分:1)

检查forEach中的项目

golds = {"Italy": 12, "USA": 33, "Brazil": 15, "China": 27, "Spain": 19, "Canada": 22, "Argentina": 8, "England": 29}

countries = []
accum = 0
for item in golds:
    countries.append(item)

有时候items =“ $ {pools}”会有多余的空间,或者它的作用类似于字符串,重新键入它应该可以解决问题。

答案 3 :(得分:0)

EL按照说明解释${class.name}-假设您使用生成getter / setter的显式或隐式方法,名称将变为getName()

您可以通过将名称明确标识为函数来覆盖此行为: ${class.name()}这将直接调用函数name()而不进行修改。

答案 4 :(得分:-1)

我在JSP上遇到相同的错误,并且错误的答案是正确的

我有以下内容:

  

并得到以下错误:

  

javax.el.PropertyNotFoundException:在类型上找不到属性“ agent”   java.lang.String

删除$ {userList}之前的空格即可解决我的问题

如果有些人有同样的问题,他会很快找到这篇文章,并且不会浪费3天的时间来寻求帮助。

答案 5 :(得分:-2)

我遇到了类似的问题: 代码段:

<c:forEach items="${orderList}" var="xx"> ${xx.id} <br>
</c:forEach>

在这样的订单列表后面有一个空格:&#34; $ {orderList}&#34; 因为xx变量被转换为String并且无法调用xx.id。

所以要确保空间。他们有时扮演重要角色。 :P