user1766169 user1766169 - 1 year ago 244
Java Question

Spring MVC Form - Request method 'GET' not supported

I am trying to learn Spring MVC but I don't know how to solve this problem.

Why do I get "Request method 'GET' not supported" when I got to URL "http://localhost:8080/SpringTest3/addStudent.html"?

package com.springtest3;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

public class StudentController {

@RequestMapping(value = "/students", method = RequestMethod.GET)
public ModelAndView showStudent() {
return new ModelAndView("student", "command", new Student());

@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("student") Student student, BindingResult result/*, final ModelMap model*/) {
if (result.hasErrors()) {
System.out.println("ERROR ERROR ERROR");

/*model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());*/

System.out.println("Name: " + student.getName());

return "redirect:students.html";


<%@taglib uri="" prefix="form"%>
<title>Spring MVC Form Handling</title>

<h2>Student Information</h2>
<form:form method="post" action="addStudent.html">
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age" /></td>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
<td colspan="2">
<input type="submit" value="Submit"/>


As suggested below I added this method in

@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
public void test(Model model) {
model.addAttribute("student", new Student());

But I also need to change the following line in addStudent.jsp:

<form:form method="post" action="addStudent.html">


<form:form method="post" modelAttribute="student" action="addStudent.html">

Answer Source

This is because when you put http://localhost:8080/SpringTest3/addStudent.html in the url, a GET request is initiated. However, the addStudent method in your controller has

@RequestMapping(value = "/addStudent", method = RequestMethod.POST)

which filters to only allow POST requests to access this method.

In order to allow GET requests, change the Mapping for another addStudent method with

@RequestMapping(value = "/addStudent", method = RequestMethod.GET) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download