1)我在一个HTML页面中有一个链接,它模拟一个HTTP GET
请求(通过手动提供查询字符串)。这个页面基本上调用一个Servlet,其中数据库访问是在它的doGet()
方法中完成的。SQL查询作为查询字符串的一部分发送。
http://localhost:8080/prelim_db_test_19_3_2013/md?q=select * from test.mobile
(test
是MySQL DB,mobile
是表)。
2)然后将结果存储在ArrayList中。
ArrayList <mobile> mylist=new ArrayList<mobile> ();
mylist.add(new mobile(rs.getInt("id"),rs.getString("name")));
其中mobile
只是一个类,其中id
和name
作为类变量(以及构造函数)
3)然后将ArrayList
转发到另一个JSP,如:
request.setAttribute("answer", mylist);
request.getRequestDispatcher("info.jsp").forward(request, response);
4)在第二个JSP中,我简单地使用<c:forEach>
迭代和打印表内容:
<c:forEach items="${answer}" var="i">
<tr>
<td>${i.id}</td>
<td>${i.name}</td>
</tr>
</c:forEach>
然而,当我运行该项目时,会引发一个异常:
javax.el.PropertyNotFoundException: Property 'id' not found on type mobile
在这一行:
request.getRequestDispatcher("info.jsp").forward(request, response);
注意,当结果打印到控制台时,DB访问本身工作正常(因此Servlet也会使用我的查询字符串进行调用)。下面是我在Eclipse中的项目设置(如果设置方式有任何问题):
我在这里做错什么了?
编辑 mobile.java
public class mobile {
int id;
String name;
public mobile(int i,String n) {
id=i;
name=n;
}
}
发布于 2013-03-20 14:59:33
在移动类中,您需要为fields.Then添加setter和getter,只有您可以在jsp.I中访问,我认为您没有为它们保留setter和getter。
发布于 2013-03-20 15:51:17
正如@PSR和@CaptainGouLash正确指出的那样,getter和setter需要设置,以便可以通过JSTL访问类变量。
见:这和正如@CaptainGouLash指出的
https://stackoverflow.com/questions/15526887
复制相似问题