Solved

Spring MVC 3.0 - form submits to two different methods in same Controller

Posted on 2010-09-15
7
1,008 Views
Last Modified: 2013-11-11
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</form:label></td>
            <td><form:input path="email" /></td>
      </tr>
      <tr>
            <td><form:label path="lastname">Telephone</form:label></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  
@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());
	}
}

Open in new window

0
Comment
Question by:lotrzz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 33688833
try giving the input a name

<input type="submit" name="add" value="Add"/>

@RequestMapping(value = "/addContact", params = "add", method = RequestMethod.POST)
0
 

Author Comment

by:lotrzz
ID: 33689049
org.springframework.web.servlet.PageNotFound - 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"
0
 
LVL 92

Expert Comment

by:objects
ID: 33689084
> /SpringMVC/contacts.html]

your path is wrong, you don't have anything mapped to /contacts.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lotrzz
ID: 33689144
What is the correct path?  That looks like the path I have in my servlet
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 33689159
What servlet?

>      @RequestMapping("/contacts")
>      public ModelAndView showContacts() {

showContents() is mapped to /contacts
0
 

Author Comment

by:lotrzz
ID: 33689185
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>


@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());
	}
}

Open in new window

0
 

Author Closing Comment

by:lotrzz
ID: 33689244
showContacts was also responsible for loading the page, and that mapping was wrong. Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
wordappend challenge 8 247
stackato and cloud 4 109
spring maven example issues 3 99
Hibernate Inheritance Strategy for Abstract Base class and two concrete classes 3 38
Hi,      I've been learning Spring and Jersey for the past few months and to say in simple, i am pretty much impressed with these frameworks. Many developers feel it awkward to implement a RESTful Web Services with such a popular Web Application Fr…
WHY MVC is the future technology... As many of you know, MVC is Model,View,Controller pattern. Model View Controller pattern was invented in a Smalltalk context at Xerox in late 1970s. Why is this buzz about MVC: --------------------------- …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question