-1

我试图从数据库中删除一个用户,但徒劳无功......我能够将用户保存到数据库中。

我的弹簧控制器类如下

CController.java

import project4.UserDAO1;
import project4.User1;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.validation.BindingResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;


@Controller




public class CController{

    private UserDAO1 userDAO;
    @Autowired
    @Qualifier("myUserDAO")
    private UserDAOImpl1 myUserDAO;

    public void setUserDAO(UserDAO1 userDAO) {
        this.userDAO = userDAO;
    }

    @RequestMapping(value = "/frm4/add", method = RequestMethod.POST)
   public ModelAndView add( @ModelAttribute("add") User1 user,HttpServletRequest 
       request,HttpServletResponse response) throws Exception {
         System.out.println("hai");

        userDAO.saveUser(user);
        System.out.println("hai");
        return new ModelAndView("redirect:/list.htm");
    }

    @RequestMapping(params = "/deleteuser/delete", method = RequestMethod.POST)
    @Transactional
    public ModelAndView delete(@ModelAttribute("delete") User1 user,HttpServletRequest 
          request,HttpServletResponse response) throws Exception {
          userDAO.deleteUser(user);
          return new ModelAndView("redirect:list.htm");
    }


    @RequestMapping(params = "find", method = RequestMethod.POST)
    @Transactional
    public ModelAndView find(@ModelAttribute("find") User1 user,HttpServletRequest 
        request,HttpServletResponse response) throws Exception {  
                     userDAO.findUser(user);
                      return new ModelAndView("redirect:list.htm");
    }


    @RequestMapping(params = "update", method = RequestMethod.POST)
    @Transactional
    public ModelAndView update(@ModelAttribute("update") User1 user,HttpServletRequest
          request,HttpServletResponse response) throws Exception {  
                     userDAO.updateUser(user);
                      return new ModelAndView("redirect:list.htm");
    }


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list(HttpServletRequest request,
            HttpServletResponse response) throws Exception {


        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("userList", userDAO.listUser());
        modelMap.addAttribute("user", new User1());
        return new ModelAndView("list", modelMap);
    }
  }

我的休眠模板类如下

import project4.User1;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class UserDAOImpl1 implements UserDAO1 {

    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, readOnly=false)
    public void saveUser(User1 user) {
    try {
     System.out.println (user.getId ());
    hibernateTemplate.save(user);
    }catch (RuntimeException re){
        throw re;
    }
  }

  @Override
  @SuppressWarnings("unchecked")
  public List<User1> listUser() {
    List<User1> result = hibernateTemplate.find("from User1");
    System.out.println("hai");
    System.out.println(result);
    return result;
  }

    @Override
   public void deleteUser(User1 user) {
       hibernateTemplate.delete("from User1 where USER_ID=:" +user.getId());
   }

    @Override
    public List<User1> findUser(User1 user) {
        List<User1> result =hibernateTemplate.find("from User1 where USER_ID=:" 
                          +user.getId());
        return result;

    }

    @Override
    public void updateUser(User1 user) {
        hibernateTemplate.update(user);

    }

 }

我的前端jsp如下

frm4.jsp

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Insert title here</title>
 </head>
 <body>
   <form method="POST" action="<%=request.getContextPath()%>/frm4/add">
    <table>
     <tr>
      <td><label for="oid">Id:</label></td>
      <td>
          <input  name="oid" value="${user.id}" />
      </td>
     </tr>
     <tr>
     <td>
        <label for="name">Name:</label></td>
     <td>
        <input name="name" value="${user.name}" />
     </td>
    </tr>
    <tr>
     <td>
        <label for="password">Password:</label></td>
     <td>
         <input name="password" value="${user.password}" />
     </td>
    </tr>

    <tr>
      <td>
          <label for="gender">Gender:</label></td>
      <td>
          <input name="gender" value="${user.gender}" />
      </td>
     </tr>
     <tr>
       <td>
           <label for="lastName">Country:</label></td>
       <td>
            <input name="country" value="${user.country}" />
       </td>
      </tr>

    </table> 
      <input type="submit" value="Submit"/> 
   </form>
  </body>
 </html>

实现删除功能的jsp如下

删除用户.jsp

<%@ page import="java.util.Date" %>
<%@ page isELIgnored="false" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     <title>Insert title here</title>
 </head>
 <body>
     <form method="POST" action="<%=request.getContextPath()%>/deleteuser/delete"> 

      <P>Are you sure you want to delete this user??</P>
       <input type="submit" value="Yes" />

   </form>
     <p> <a href="frm4.jsp">No</a></p>
   </body>
  </html>

如果我使用,我会收到“既没有绑定结果也没有普通目标对象”错误,所以我只是使用 jsp 表单标签。我对两个 jsp 文件使用相同的实现方法,但 deleteuser.jsp 没有映射到 spring 控制器类即CController.java

当我单击删除按钮时,我得到了 url

http://localhost:8080/Spring/deleteuser/delete

它只是搜索文件删除而不是映射到控制器。任何帮助 plzzz

4

1 回答 1

1

当您通过 deleteuser.jsp 提交时,您没有 User1 类型的模型属性“delete”。您只有一个带有一个提交按钮的表单。

如果您正在实现确认页面,请尝试在会话或其他机制上保留 User1 对象。

通常你不需要绑定整个对象来删除一个条目,你可以传递用户 id 并通过控制器上的 hibernate 查找它

于 2013-05-30T06:16:56.953 回答