javax.el.PropertyNotFoundException: Property 'foo' not found on type com.example.Bean
Asked Answered
K

6

32

I have results from

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

query and I would like to display it on JSP.

I have loop:

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

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

</c:forEach>

and I would like to display results from poolQuestion table (which is Join table). The value that i want to display is 'answer'.

How can I do it?

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

The above code doesn't works.

The error is:

  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)

The model is:

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();

The controller is:

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

model.addAttribute("pools", pool);

And the View is:

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

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

</c:forEach>

PS. the ${pool.name} is displayed properly

Kinesics answered 20/12, 2011 at 15:12 Comment(3)
Your error information is incomplete. It's basically telling that "an exception" occurred, but you didn't show the exception at all. The answer is inside the exception. My first guess would be that the poolQuestion property has to be fetched eagerly instead of lazily. But again, it's the real exception which contains the answer.Fairman
the error shows me only, that there is no answer - but i can't to the answer becose i can't display whole list structure.... I've added full error upper...Kinesics
Please note that the code in the error message does not match the code in your attempt.Fairman
F
94

javax.el.PropertyNotFoundException: Property 'foo' not found on type com.example.Bean

This literally means that the mentioned class com.example.Bean doesn't have a public (non-static!) getter method for the mentioned property foo. Note that the field itself is irrelevant here!

The public getter method name must start with get, followed by the property name which is capitalized at only the first letter of the property name as in Foo.

public Foo getFoo() {
    return foo;
}

You thus need to make sure that there is a getter method matching exactly the property name, and that the method is public (non-static) and that the method does not take any arguments and that it returns non-void. If you have one and it still doesn't work, then chances are that you were busy editing code forth and back without firmly cleaning the build, rebuilding the code and redeploying/restarting the application. You need to make sure that you have done so.

For boolean (not Boolean!) properties, the getter method name must start with is instead of get.

public boolean isFoo() {
    return foo;
}

Regardless of the type, the presence of the foo field itself is thus not relevant. It can have a different name, or be completely absent, or even be static. All of below should still be accessible by ${bean.foo}.

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

You see, the field is not what counts, but the getter method itself. Note that the property name itself should not be capitalized in EL. In other words, ${bean.Foo} won't ever work, it should be ${bean.foo}.

See also:

Fairman answered 20/12, 2011 at 15:24 Comment(8)
OK, but there is one small problem. My Query is join left query. Answer variable is on the table i mysql which is joined.Kinesics
This has nothing to do with HQL. This has to do with your model (the javabean class). There is no such method on the com.pool.app.domain.Pool class. You need to make sure that it has that method or to make sure that you reference the proper property name in EL.Fairman
ok, so from the begining - i have two tables in my mysql. Those tables are mapped to POJO files. One table is Pool and Second is PoolQuestion. In Pool POJO there is no getAnswer, becouse 'answer' is in PoolQuestion table and it is mapped in PoolQuestion. So - im making query based on Pool.java (no getAnswer) and left join PoolQuestion.java (there is getAnswer). I'm sending the response to controller based on Pool.java and then to the view, so as i understand there can't be 'getAnswer' becouse there is no 'answer' in Pool POJO. But what to do now...Kinesics
Well, then add a getPoolQuestion() to Pool class and use ${pool.poolQuestion.answer} then as in your error message? That said, I suggest to get yourself through a basic tutorial/book and put all concepts separately instead of seeing it as one whole thing. The model design problem has in turn nothing to do with JSP/JSTL/EL.Fairman
After way too much time looking this, I googled it and fount this. Very helpful... However seems like a rather perculiar way to set this up...Langobard
After one hour of research and tons of advices like "Get your naming right" my fault was that the getter-method was privat...+1 Thanks!Danita
@PhilipRego: ".. capitalized at only the first letter ..". So only the first letter. Capitalization of the first letter of "fooTwo" results in "FooTwo".Fairman
"The public getter method name must start with get, followed by the property name which is capitalized at only the first letter of the property name as in Foo." Not knowing this rule costs me hours of frustration. Many thanks to BalusC.Kuwait
K
6

I believe the id accessors don't match the bean naming conventions and that's why the exception is thrown. They should be as follows:

public Integer getId() { return id; }    
public void setId(Integer i){ id= i; }
Kennedy answered 18/1, 2015 at 15:27 Comment(0)
T
2

Check the items in forEach

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

        ${pool.name}

    </c:forEach>

Some times items="${pools}" has an extra space or it acts like string, retyping it should solve the issue.

Thoroughbred answered 12/5, 2020 at 15:58 Comment(0)
M
1

EL interprets ${class.name} as described - the name becomes getName() on the assumption you are using explicit or implicit methods of generating getter/setters

You can override this behavior by explicitly identifying the name as a function: ${class.name()} This calls the function name() directly without modification.

Mcclellan answered 18/8, 2018 at 23:38 Comment(0)
S
0

I get the same error on my JSP and the bad rated answer was correct

I had the folowing line:

<c:forEach var="agent" items=" ${userList}" varStatus="rowCounter">

and get the folowing error:

javax.el.PropertyNotFoundException: Property 'agent' not found on type java.lang.String

deleting the space before ${userList} solved my problem

If some have the same problem, he will find quickly this post and does not waste 3 days in googeling to find help.

Spelter answered 12/10, 2018 at 12:21 Comment(0)
P
-2

I was facing the similar type of issue: Code Snippet :

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

There was a space after orderlist like this : "${orderList} " because of which the xx variable was getting coverted into String and was not able to call xx.id.

So make sure about space. They play crucial role sometimes. :p

Prussia answered 3/2, 2018 at 22:12 Comment(1)
This does not answer the question, where there clearly is no such extra space.Brauer

© 2022 - 2024 — McMap. All rights reserved.