我在课堂上有这种方法
public ArrayList<Comuna> getComunas() { ConexionBd(); try { comunas = new ArrayList<Comuna>(); resultado = consulta.executeQuery("select * from comuna"); while(resultado.next()) { comunas.add(new Comuna(resultado.getInt("com_id"),resultado.getInt("pro_id"),resultado.getString("com_nombre"))); } CerrarConexionBd(); } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } return comunas; }
我试图把价值放在这里
<c:set var="variable" value="${Conexion.getComunas}"></c:set>
并用这个显示值
<select class="form-control" id="comuna" name="comuna"> <c:forEach var="comunas" items="${variable} " > <option value="${comunas.com_id}">${comunas.com_nombre}</option> </c:forEach> </select>
我收到此错误javax.el.PropertyNotFoundException:该类'java.lang.String'没有属性’com_id’
javax.el.PropertyNotFoundException
'java.lang.String'
at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:229) at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:206) at javax.el.BeanELResolver.property(BeanELResolver.java:317) at javax.el.BeanELResolver.getValue(BeanELResolver.java:85) at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104) at org.apache.el.parser.AstValue.getValue(AstValue.java:183) at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185) at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026) at org.apache.jsp.formulario_005fempresa_jsp._jspx_meth_c_005fforEach_005f0(formulario_005fempresa_jsp.java:157) at org.apache.jsp.formulario_005fempresa_jsp.access$1(formulario_005fempresa_jsp.java:138) at org.apache.jsp.formulario_005fempresa_jsp$Helper.invoke1(formulario_005fempresa_jsp.java:230) at org.apache.jsp.formulario_005fempresa_jsp$Helper.invoke(formulario_005fempresa_jsp.java:307) at org.apache.jsp.tag.webplantillaformulario_tag.doTag(webplantillaformulario_tag.java:130) at org.apache.jsp.formulario_005fempresa_jsp._jspx_meth_t_005fplantillaformulario_005f0(formulario_005fempresa_jsp.java:112) at org.apache.jsp.formulario_005fempresa_jsp._jspService(formulario_005fempresa_jsp.java:85) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)`
这是我班的Comuna
public class Comuna { int com_id; int pro_id; String com_nombre; public Comuna(int com_id, int pro_id, String com_nombre) { super(); this.com_id = com_id; this.pro_id = pro_id; this.com_nombre = com_nombre; } public Comuna(){ } public int getCom_id() { return com_id; } public void setCom_id(int com_id) { this.com_id = com_id; } public int getPro_id() { return pro_id; } public void setPro_id(int pro_id) { this.pro_id = pro_id; } public String getCom_nombre() { return com_nombre; } public void setCom_nombre(String com_nombre) { this.com_nombre = com_nombre; }
}
我认为此错误与数据类型有关,但我无法解决,因为因为我正在学习中,谢谢
我收到此错误javax.el.PropertyNotFoundException:该类java.lang.String没有该属性com_id
java.lang.String
com_id
问题是在下面的行中,错误地 添加了空格,${variable}这就是为什么它被覆盖String并comunas变成类型String并且String没有任何属性的原因com_id。
${variable}
String
comunas
<c:forEach var="comunas" items="${variable} " >
只需 删除多余的结尾空间即可 解决您的问题。
下一行可能还有一个问题要访问Comunasfrom Conexion。
Comunas
Conexion
它应该是
<c:set var="variable" value="${Conexion.getComunas()}"></c:set>
要么
<c:set var="variable" value="${Conexion.comunas}"></c:set>
<c:set var="variable" value="${Conexion['comunas']}"></c:set>