web-dev-qa-db-fra.com

javax.el.PropertyNotFoundException: propriété 'foo' introuvable sur le type com.example.Bean

J'ai des résultats de

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

requête et je voudrais l'afficher sur JSP.

J'ai boucle:

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

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

</c:forEach>

et j'aimerais afficher les résultats de la table poolQuestion (qui est une table jointe). La valeur que je veux afficher est "répondre".

Comment puis-je le faire?

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

Le code ci-dessus ne fonctionne pas.

L'erreur est:

  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)

Le modèle est:

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

Le contrôleur est:

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

model.addAttribute("pools", pool);

Et la vue est:

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

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

</c:forEach>

PS. le ${pool.name} est affiché correctement

20
Ilkar

javax.el.PropertyNotFoundException: propriété 'foo' introuvable sur le type com.example.Bean

Cela signifie littéralement que la classe mentionnée com.example.Bean ne possède pas de méthode de lecture publique (non statique!) Pour la propriété mentionnée foo. Notez que le champ lui-même n'est pas pertinent ici!

Le nom de la méthode getter publique doit commencer par get, suivi du nom de la propriété en majuscule à la première lettre du nom de la propriété, comme dans Foo.

public Foo getFoo() {
    return foo;
}

Vous devez donc vous assurer qu’une méthode getter correspond exactement au nom de la propriété, que la méthode est public (non -static) et qu’elle ne prend aucun argument et qu’elle renvoie non -void. Si vous en avez un et que cela ne fonctionne toujours pas, il y a des chances pour que vous ayez été occupé à éditer le code sans avoir à nettoyer la construction, à reconstruire le code et à redéployer/redéployer l'application. Vous devez vous assurer que vous l'avez fait.

Pour les propriétés boolean (pas Boolean!), Le nom de la méthode d'accesseur en lecture doit commencer par is au lieu de get.

public boolean isFoo() {
    return foo;
}

Quel que soit le type, la présence du champ foo lui-même est donc non pertinente. Il peut avoir un nom différent, ou être complètement absent, ou même être static. Tous les éléments ci-dessous doivent toujours être accessibles par ${bean.foo}.

public Foo getFoo() {
    return bar;
}

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

public Foo getFoo() {
    return FOO_CONSTANT;
}

Vous voyez, le champ n'est pas ce qui compte, mais la méthode getter elle-même. Notez que le nom de la propriété elle-même ne doit pas être capitalisé dans EL. En d'autres termes, ${bean.Foo} ne fonctionnera jamais, ce devrait être ${bean.foo}.

Voir également:

61
BalusC

Je crois que les accesseurs d'identifiant ne correspondent pas aux conventions de nommage des beans et c'est pourquoi l'exception est levée. Ils devraient être comme suit:

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

EL interprète ${class.name} comme décrit - le nom devient getName () en supposant que vous utilisez des méthodes explicites ou implicites de génération de getter/setters.

Vous pouvez remplacer ce comportement en identifiant explicitement le nom en tant que fonction: ${class.name()} Ceci appelle la fonction name () directement sans modification. 

0
sdw