我正在尝试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表单标签(因为它使我“既没有绑定也没有普通目标对象”错误)。因此,当我运行此程序时,出现以下错误
type Status report
message
description The requested resource () is not available.
我得到的URL为
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">
但错误仍然相同
最佳答案
您尚未为/add
定义任何请求映射
您只有/frm4
,并且按参数名称(例如添加,删除等)分隔操作。
为此,请使用action="/frm4"
,并在表单中添加一个隐藏的参数名称add
。
但我建议将其更改为单独的网址,例如frm4 / add,frm4 / delete等。
@RequestMapping(value = "/add", method = RequestMethod.POST)