我正在尝试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”文件夹下。我已在WebContent下放置了frm4.jsp和list.jsp。
编辑:
我试过
<form method="POST" action="CController/add.do">
和
<form method="POST" action="Spring/CController/add.do">
但错误仍然相同
答案 0 :(得分:1)
您尚未为/add
您只有/frm4
,并且您正在通过添加,删除等参数名称来分隔操作。
要使其工作,请使用action="/frm4"
并在表单中添加隐藏的参数名称 add
。
但我建议将其更改为单独的网址,例如 frm4 / add,frm4 / delete等。
@RequestMapping(value = "/add", method = RequestMethod.POST)
答案 1 :(得分:1)
man你必须将表单中的操作设置为“/ frm4”
并更改
<input type="submit" value="Submit"/>
到
<input type="submit" name="add" value="Submit"/>
和
@ModelAttribute("add") User1 user
到
@ModelAttribute User1 user
请查看这些videos
答案 2 :(得分:0)
在弹簧配置中使用<context:component-scan base-package="<YOUR PACKAGE HERE>"/>
进行弹簧扫描注释(如果尚未完成)。