java Spring MVC 表单 - 不支持请求方法“GET”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29866130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:02:58  来源:igfitidea点击:

Spring MVC Form - Request method 'GET' not supported

javaspringspring-mvc

提问by user1766169

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

我正在尝试学习 Spring MVC 但我不知道如何解决这个问题。

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

当我到达 URL“ http://localhost:8080/SpringTest3/addStudent.html”时,为什么会得到“不支持请求方法‘GET’ ”?

StudentController.java:

学生控制器.java:

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;

@Controller
@SessionAttributes
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";
   }
}

addStudent.jsp:

添加Student.jsp:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

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


Solution:
As suggested below I added this method in StudentController.java:


解决方案:
如下所示,我在 StudentController.java 中添加了此方法:

   @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:

但我还需要更改 addStudent.jsp 中的以下行:

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

to

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

回答by SMA

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

当我到达 URL“ http://localhost:8080/SpringTest3/addStudent.html”时,为什么会出现“不支持请求方法‘GET’ ”

Because you have defined it to accept post method like:

因为您已将其定义为接受 post 方法,例如:

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

And when you invoke it directly say via browser, you are accessing the resource using HTTP Get method.

当您通过浏览器直接调用它时,您正在使用 HTTP Get 方法访问资源。

If you change this to RequestMethod.GET, you would avoid the error you would see. But Post seems appropriate from what it means, does. So access it via form that you paste rather than from URL or to test it, use appropriate http client and set http method to POST.

如果您将其更改为 RequestMethod.GET,您将避免您将看到的错误。但是 Post 从它的意思来看似乎很合适,确实如此。因此,通过您粘贴的表单而不是从 URL 访问它或测试它,使用适当的 http 客户端并将 http 方法设置为 POST。

回答by AmmarCSE

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

这是因为当你在 url 中输入http://localhost:8080/SpringTest3/addStudent.html时,会发起一个 GET 请求。但是,控制器中的 addStudent 方法具有

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

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

哪些过滤器仅允许 POST 请求访问此方法。

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

为了允许 GET 请求,更改另一个 addStudent 方法的映射

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