使用Hibernate无法从数据库检索数据到jsp

时间:2016-02-17 14:36:47

标签: java hibernate jsp

我试图在表中显示数据,从Oracle数据库中检索它但它不会显示出来。

下面是使用的代码。

要列出数据的Servlet:

package org.sistemamedico.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sistemamedico.db.Conexion;

@WebServlet("/ServletListarMedicamento.do")
public class ServletListarMedicamento extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {
    RequestDispatcher despachador = null;
    req.setAttribute("listaMedicamento", Conexion.getInstancia().listar("From Medicamento"));
    despachador = req.getRequestDispatcher("sistemamedico/farmacia.jsp");
    despachador.forward(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(req, resp);
}
}

与列表方法的数据库连接:

package org.sistemamedico.db;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class Conexion {
private SessionFactory sesion;
private static Conexion instancia;
public static synchronized Conexion getInstancia(){
    return (instancia==null)?new Conexion():instancia;
}
public Conexion(){
    try {
        sesion=new Configuration().configure().buildSessionFactory();
    } catch (Throwable e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}
public void closeSession()throws HibernateException {
    try {
        if (sesion.isClosed()==false | sesion.getCurrentSession().isOpen()) {
            sesion.close();
            sesion.getCurrentSession().close();
        }
    } catch (HibernateException ex) {
        throw new HibernateException(ex);
    }
}

public List<Object> listar(String consulta){
    Session miSesion=sesion.getCurrentSession();
    List<Object> lista=null;
    miSesion.beginTransaction();
    lista=miSesion.createQuery(consulta).list();
    miSesion.getTransaction().commit();
    return lista;
}
public List<Object> autenticarUsuario(String nick,String contraseña){
    Session miSesion=sesion.getCurrentSession();
    List<Object> lista=null;
    miSesion.beginTransaction();
    lista=miSesion.createQuery("From Usuario u where u.nick='"+nick+"' and contraseña='"+contraseña+"'").list();
    miSesion.getTransaction().commit();
    return lista;
}
public void agregar (Object agregar){
    Session session=sesion.getCurrentSession();
    session.beginTransaction();
    session.save(agregar);
    session.getTransaction().commit();  
}
public Object buscar(Class<?> clase,int id){
    Session session=sesion.getCurrentSession();
    session.beginTransaction();
    Object obj=session.get(clase, id);
    session.getTransaction().commit();
    return obj;
}
public Object buscar(Class<?> clase,String id){
    Session session=sesion.getCurrentSession();
    session.beginTransaction();
    Object obj=session.get(clase, id);
    session.getTransaction().commit();
    return obj;
}
}

我在JSP中的表:

<table class="table table-hover">
              <thead>
                <tr>
                  <th>Nombre</th>
                  <th>Descripcion</th>
                  <th>Proveedor</th>
                  <th>Fecha Vencimiento</th>
                  <th>Precio</th>
                </tr>
               </thead>
               <tbody>
                  <c:forEach items="${listaMedicamento}" var="medicamento">
                    <tr>
                        <td>${medicamento.getNombre()}</td>
                        <td>${medicamento.getDescripcion()}</td>
                        <td>${medicamento.getIdProveedor().getNombre()}</td>
                        <td>${medicamento.getFechaVencimiento()}</td>
                        <td>${medicamento.getPrecio()}</td>
                    </tr>
                 </c:forEach>
                </tbody>
            </table>

我已经把taglib放到了c:foreach可以工作,但它不会向我显示任何数据。

2 个答案:

答案 0 :(得分:0)

当我使用<c:forEach>时,我使用<c:out>来显示每个单元格。看看是否有效。

<tbody>
    <c:forEach items="${listaMedicamento}" var="medicamento">
                <tr>
                    <td><c:out value="${medicamento.getNombre()}"/></td>
                    <td><c:out value="${medicamento.getDescripcion()}"/></td>
                    <td><c:out value="${medicamento.getIdProveedor().getNombre()}"/></td>
                    <td><c:out value="${medicamento.getFechaVencimiento()}"/></td>
                    <td><c:out value="${medicamento.getPrecio()}"/</td>
                </tr>
      </c:forEach>
</tbody>

答案 1 :(得分:0)

第1步:请通过在Servlet类中打印来尝试查看db中的列表数据是否正确。
步骤2:如果数据正确,请检查列表中的对象类型/结构,并验证您是否正在JSP中正确迭代它。
Step3:如果列表为空,我们必须修改查询并进一步调试。
示例:列表listFromDb = Conexion.getInstancia()。listar(&#34;来自Medicamento&#34;); System.out.println(&#34;来自db的数据是:&#34; + listFromDb); req.setAttribute(&#34; listaMedicamento&#34;,listFromDb);