关于list的封装与在jsp页面的获取
查询表中的数据
将list保存在request中,一定要注意要跳转的方式,因为这样request才能将list中的参数传过来,再用
request.setAttribute("list",list);
request.getRequestDispatcher("/jsp/customer/show.jsp").forward(request,response);
这里是封装在session中,在session中就不用这么严格,当然对比一下,这个路径也要对。
request.getSession().setAttribute("list", list);
response.sendRedirect("/hibernate1124/jsp/customer/show.jsp");
获取将list封装在session或request中的循环输出与EL表达式在jsp页面输出
<c:forEach items="${list}" var="item" varStatus="status">
<tr >
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.age}</td>
</tr>
</c:forEach>
这里使用hibernate对后台数据操作,特别注意里面的 from Student 是你的实体类的名字, System.out.println(list);这句是显示在控制台,看知否能够获取数据,来做个检验,如果有数据,那很有可能是输出的代码出错了,参数没有传到jsp页面;
public List<Student> queryAllEmp() {
Session session=HibernateUtils.openSession();
Transaction tr=session.beginTransaction();
List<Student> list =session.createQuery("from Student").list();//查询全部
tr.commit();//提交事务
System.out.println(list);
return list;
}
这个是我最近才学,有不足的地方,请各位指出来哈,我们共同进步,嘿嘿