使用href链接从java servlet和hibernate

时间:2016-12-03 16:07:33

标签: java hibernate servlets

我在互联网上寻找答案,但我不理解所提供的解决方案,所以这是我的最后一招:基本上我有一张桌子,我想添加,删除和编辑记录。在jsp我的代码是:

 <c:forEach items="${All}" var="p">
             <tr>
                 <td>${p.id}</td>
                 <td>${p.firstName}</td>
                 <td>${p.middleName}</td>
                 <td>${p.lastName}</td>
                 <td>${p.email}</td>
                 <td><a href="edit?id=${p.id}">Edit</a>
                 <a href="delete?id=${p.id}">Delete</a></td>

             </tr>
             </c:forEach>

但是当我点击删除时,它会将我重定向到404状态代码和带删除的链接?id = 1。那么如何将链接转到我删除行的DeletePost servlet,然后重定向到显示表的页面?

编辑:在我的网络应用程序中,我试图删除数据库中的一行,但它似乎没有用,我试图找出原因,但我不能。这是我的道教课程:

public class userDao  {

private SessionFactory factory, session;

public userDao(SessionFactory factory) {
    this.factory = factory;
}

public void delete(long id){
    Session session1 = factory.openSession();
    Transaction tx = null;
    List<User> u = null;
    try {
        tx = session1.beginTransaction();
        Query query = session1.createQuery("SELECT * FROM user_table WHERE id = :id");
        query.setParameter("id", id);
        System.out.println("aaaaa" + id);
        u = query.list();
        session1.delete(u.get(0));
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }

    } finally {
        session1.close();
    }

}
}

在我的servlet中我有:

public class DeleteServlet extends HttpServlet{
userDao u = new userDao(new Configuration().configure().buildSessionFactory());
User a = new User();
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
    System.out.println("asa");
    response.sendRedirect("/A1.2/home.jsp");
}


@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
    String s = request.getParameter("id");
    long id = Long.parseLong(s);
    u.delete(id);

    response.sendRedirect("/A1.2/home.jsp");
}
}

我认为问题出现在dao中,因为我试图从数据库中获取用户并获得空指针异常。

0 个答案:

没有答案