我有一个Java bean类如下
public class Users extends dbConnect
{
private int UserId;
public int getUserId()
{
return UserId;
}
public void setUserId(int userId)
{
UserId = userId;
}
}
我的 Servlet 中有一个 doGet 方法,它获取 javabean 类中的值并显示在 JSP 页面中。ListUsers 方法将从数据库中返回一个列表。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List<Users> arrUserList = new ArrayList<Users>();
Users objUser = new Users();
arrUserList = objUser.listUsers();
for(Users Userobj : arrUserList)
{
System.out.println(Userobj.getUserId());
}
request.setAttribute("arrUserDetails", arrUserList);
RequestDispatcher rst = request.getRequestDispatcher("ListUsers.jsp");
rst.forward(request, response);
}
当我在 for 循环中打印 UserId 的值时,它显示 UserId。但它没有显示在我的 JSP 页面中。
<%@ page import="com.acme.users.Users"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:forEach items="${arrUserDetails}" var="account">
<tr>
<td>
<c:out value="${account.UserId}" />
</td>
</tr>
</c:forEach>
它显示javax.el.PropertyNotFoundException。我检查了堆栈中的异常并确保 bean 名称和 getters 方法相同。但它仍然显示错误。
控制台中的消息如下
Feb 16, 2013 2:44:19 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
javax.el.PropertyNotFoundException: Property 'UserId' not found on type com.acme.users.Users
at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:223)
at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:200)
at javax.el.BeanELResolver.property(BeanELResolver.java:311)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
at org.apache.jsp.ListUsers_jsp._jspx_meth_c_005fout_005f0(ListUsers_jsp.java:170)
at org.apache.jsp.ListUsers_jsp._jspx_meth_c_005fforEach_005f0(ListUsers_jsp.java:124)
at org.apache.jsp.ListUsers_jsp._jspService(ListUsers_jsp.java:84)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at com.acme.users.ListUsers.doGet(ListUsers.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
为什么会这样?