0

我正在尝试 spring mvc 示例,但无法将控件从 jsp 转移到控制器类。我的控制器类如下

CController.java

package project4;

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
@RequestMapping("/frm4")


public class CController{

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

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

    @RequestMapping(params = "add", method = RequestMethod.POST)
    public ModelAndView add( @ModelAttribute("add") User1 user,HttpServletRequest 
            request,HttpServletResponse response) throws Exception {
        userDAO.saveUser(user);
        System.out.println("hai");
        return new ModelAndView("redirect:list.htm");
    }

    @RequestMapping(params = "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");
    }

     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("userForm", modelMap);
    }
 }

而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="add.do">
   <table>
     <tr>
        <td><label for="id">Id:</label></td>
        <td>
            <input  name="id" 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中使用spring form标签(因为它抛出了“既不是绑定也不是普通目标对象”错误)。所以当我运行这个程序时,我收到以下错误

type Status report

message 

description The requested resource () is not available.

我得到的网址为

http://localhost:8080/Spring/add.do

其中 Spring 是我存储程序的项目名称。我已将 CController.java 控制器类放在 src 中的“project4”文件夹下。我已将 frm4.jsp 和 list.jsp 放在 WebContent 下。

编辑:

我试过了

<form method="POST" action="CController/add.do">

<form method="POST" action="Spring/CController/add.do">

但错误保持不变

4

3 回答 3

1

伙计,您必须将表单中的操作设置为“/frm4”

和改变

<input type="submit" value="Submit"/>

<input type="submit" name="add" value="Submit"/>

@ModelAttribute("add") User1 user

@ModelAttribute User1 user

请检查这些视频

于 2013-04-29T11:44:27.540 回答
1

您尚未定义任何请求映射/add

您刚刚拥有/frm4并且您正在通过参数名称(如添加、删除等)分隔操作。

为此,请使用action="/frm4"并在表单中添加一个隐藏的参数名称 add

但我建议将其更改为单独的 url ,如frm4/add、frm4/delete 等

@RequestMapping(value = "/add", method = RequestMethod.POST)
于 2013-04-29T05:52:14.357 回答
0

<context:component-scan base-package="<YOUR PACKAGE HERE>"/>如果尚未完成,请在您的 spring 配置中使用 spring 来扫描注释。

类似问题:Spring MVC 组件扫描

于 2013-04-29T05:59:52.097 回答