lotrzz
asked on
Spring MVC 3.0 - form submits to two different methods in same Controller
Is it actually possible for a form to submit to two different methods in the same controller using spring mvc 3.0 ?
I have a form
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring 3 MVC Series - Contact Manager</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Contact Manager</h2>
<form:form method="post" action="addContact">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Last Name</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Email</for m:label></ td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><form:label path="lastname">Telephone< /form:labe l></td>
<td><form:input path="telephone" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Add"/></td>
<td colspan="2"><input type="submit" value="Search"/></td>
</tr>
</table>
As you see there is a add button and a search button. I want the add go to the addContact method and serach go to the showResults method in the controller.
The controller code is attached
I have a form
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring 3 MVC Series - Contact Manager</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Contact Manager</h2>
<form:form method="post" action="addContact">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Last Name</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Email</for
<td><form:input path="email" /></td>
</tr>
<tr>
<td><form:label path="lastname">Telephone<
<td><form:input path="telephone" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Add"/></td>
<td colspan="2"><input type="submit" value="Search"/></td>
</tr>
</table>
As you see there is a add button and a search button. I want the add go to the addContact method and serach go to the showResults method in the controller.
The controller code is attached
@Controller
@SessionAttributes
public class ContactController {
public static final String SEARCH_RESULTS_NAME = "ContactSearchResults";
@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact")
Contact contact, BindingResult result,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("First Name:" + contact.getFirstname() +
"Last Name:" + contact.getLastname());
return "redirect:contacts.html";
}
@RequestMapping("/contacts")
public ModelAndView showContacts() {
return new ModelAndView("contact", "command", new Contact());
}
}
ASKER
org.springframework.web.se rvlet.Page NotFound - No mapping found for HTTP request with URI [/SpringMVC/contacts.html] in DispatcherServlet with name 'spring'
when I am trying to add the second button ( there are two buttons name="add" name="search"
when I am trying to add the second button ( there are two buttons name="add" name="search"
> /SpringMVC/contacts.html]
your path is wrong, you don't have anything mapped to /contacts.html
your path is wrong, you don't have anything mapped to /contacts.html
ASKER
What is the correct path? That looks like the path I have in my servlet
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry I meant Controller(not servlet). The code to the controller is attached and my contact.jsp has two buttons
<td colspan="2">
<input type="submit" name="add" value="Add Contact"/><br>
<input type="submit" name="search" value="Search"/>
</td>
<td colspan="2">
<input type="submit" name="add" value="Add Contact"/><br>
<input type="submit" name="search" value="Search"/>
</td>
@Controller
@SessionAttributes
public class ContactController {
@RequestMapping(value = "/addContact", params="add", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact")
Contact contact, BindingResult result) {
System.out.println("First Name:" + contact.getFirstname() +
"Last Name:" + contact.getLastname());
return "redirect:contacts.html";
}
@RequestMapping(value="/addContact", params="search")
public ModelAndView showContacts() {
System.out.println("i m here");
return new ModelAndView("contact", "command", new Contact());
}
}
ASKER
showContacts was also responsible for loading the page, and that mapping was wrong. Thanks
<input type="submit" name="add" value="Add"/>
@RequestMapping(value = "/addContact", params = "add", method = RequestMethod.POST)