Reputation: 261
I am trying spring mvc example and i am not able to transfer the control from jsp to the controller class.My controller class is as follows
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);
}
}
and the jsp page is as follows
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>
i am not using spring form tag in jsp(because it is throwing me "neither binding nor plain target object "error).So when i run this program i am getting the following error as
type Status report
message
description The requested resource () is not available.
and i am getting the url as
http://localhost:8080/Spring/add.do
where Spring is my project name under which i have stored the program.I have placed the CController.java controller class under "project4" folder in src.I have placed frm4.jsp and list.jsp under WebContent.
EDIT:
i tried
<form method="POST" action="CController/add.do">
and
<form method="POST" action="Spring/CController/add.do">
but the error remains the same
Upvotes: 0
Views: 1229
Reputation: 12942
man you have to set action in form to "/frm4"
and change
<input type="submit" value="Submit"/>
to
<input type="submit" name="add" value="Submit"/>
and
@ModelAttribute("add") User1 user
to
@ModelAttribute User1 user
please check these videos
Upvotes: 1
Reputation: 1
Use <context:component-scan base-package="<YOUR PACKAGE HERE>"/>
in your spring config for spring to scan for annotations if not already done.
Similar Issue: Spring MVC Component Scan
Upvotes: 0
Reputation: 10987
You have not defined any request mapping for /add
You just have /frm4
and you are separating actions by param names like add, delete, etc.
For this to work use action="/frm4"
and in the form add a hidden param name add
.
But I would advice to change it to separate urls like frm4/add, frm4/delete, etc.
@RequestMapping(value = "/add", method = RequestMethod.POST)
Upvotes: 1