当我使用hibernate类时,数据库表内容不会出现

时间:2016-10-07 05:29:42

标签: hibernate

POJO类与Util类一起正确创建。这里是controller和model.dao类:

package model.dao;

import java.util.List;
import model.pojo.Device;
import model.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

public class DeviceDAO {
    public static List<Device> layDS(){
        List<Device> lst=null;
        try{
            Session session=HibernateUtil.getSessionFactory().openSession();
            String hql="from device";
            Query query;
            query = session.createQuery(hql);
            lst=query.list();
            session.close();
        }catch(Exception e){
            e.printStackTrace();
        }


    return lst;}
}
//end of deviceDAO

现在是DeviceController类:

package controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.DeviceDAO;
import model.pojo.Device;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class DeviceController implements Controller{

    @Override
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
ModelAndView mv=new ModelAndView("devices");
    try{
        List<Device> lst=DeviceDAO.layDS();
        mv.addObject("devices",lst);
    }catch(Exception e){
        e.printStackTrace();
    }
    return mv;}

}
//end of deviceController class

这是devices.jsp文件:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Device List!</h1>
        <table>
            <tr>
                <th>ID</th><th>latitude</th><th>longitude</th>
            </tr>
            <c:forEach var="device" items="${devices}">
                <tr>
                    <td>
                        <c:out value="${device.getDevId()}"></c:out>
                    </td>
                    <td>
                    <c:out value="${device.getLatitude()}"></c:out>
                    </td>
                     <td>
                        <c:out value="${device.getLongitude()}"></c:out>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

//end of device.jsp file

以下是我在dispatcher-servlet.xml类中添加的两个内容:

<bean class="controller.DeviceController" id="DeviceController" />
 <prop key="devices.jsp">DeviceController</prop>

我已经测试了与数据库的连接,它运行正常,但我不确定为什么数据库中表的内容没有显示。

请帮我弄清楚我做错了什么!

0 个答案:

没有答案