我正在尝试 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">
但错误保持不变