gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:04:22 25 4
gpt4 key购买 nike

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

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

StudentController.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:

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


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

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

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

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

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

最佳答案

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

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

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

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

如果将其更改为 RequestMethod.GET,您将避免看到的错误。但从其含义来看,波斯特似乎是恰当的。因此,通过粘贴的表单而不是从 URL 访问它,或者要测试它,请使用适当的 http 客户端并将 http 方法设置为 POST。

关于java - Spring MVC 表单 - 不支持请求方法 'GET',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29866130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com