请求方法' post'不支持。弹簧胸骨
**大家好。在开发Web应用程序时,我遇到了问题。 Save Employee方法无法正常工作并丢弃相应的错误。在项目中,我使用Spring-Boot + Thymeleaf。我认为这两个文件中存在错误或配置中的问题。但是到目前为止,我还没有找到任何东西。 **
myrestController.java
@Controller
@RequestMapping("/shop")
public class myRestController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/allEmployees")
public String allEmployees(Model model) {
List<Employee> employees = employeeService.getAllEmployees();
model.addAttribute("employeesList", employees);
return "allEmployees";
}
@GetMapping("/allEmployees/{name}")
public String getEmployeeByName(@PathVariable String name, Model model) {
List<Employee> employees = employeeService.findAllByName(name);
model.addAttribute("employeesList", employees);
return "allEmployees";
}
@GetMapping("/newEmployee")
public String addEmployee(Model model) {
Employee employee = new Employee();
model.addAttribute("employee", employee);
return "addNewEmployee";
}
@RequestMapping()
public String saveEmployee(@ModelAttribute("employee") Employee employee){
employeeService.saveEmployee(employee);
return "index";
}
addnewemployee.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Add Employee</h1>
<form th:method="POST" th:action="@{shop/allEmployees}" th:object="${employee}">
<label for="name">Enter name:</label>
<input type="text" th:field="*{name}" id="name"/>
<br/>
<label for="surname">Enter surname:</label>
<input type="text" th:field="*{surname}" id="surname"/>
<br/>
<label for="department">Enter department:</label>
<input type="text" th:field="*{department}" id="department"/>
<br/>
<label for="salary">Enter salary:</label>
<input type="text" th:field="*{salary}" id="salary"/>
<br/>
<input type="submit" value="Create!">
</form>
<br><br>
</body>
</html>
**Hi everyone. I ran into a problem while developing a web application. The saveEmployee method does not work properly and throws the corresponding error. In the project I use Spring-boot + Thymeleaf. I think there is an error in these two files or a problem in the configuration. But so far I haven't found anything.
**
myRestController.java
@Controller
@RequestMapping("/shop")
public class myRestController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/allEmployees")
public String allEmployees(Model model) {
List<Employee> employees = employeeService.getAllEmployees();
model.addAttribute("employeesList", employees);
return "allEmployees";
}
@GetMapping("/allEmployees/{name}")
public String getEmployeeByName(@PathVariable String name, Model model) {
List<Employee> employees = employeeService.findAllByName(name);
model.addAttribute("employeesList", employees);
return "allEmployees";
}
@GetMapping("/newEmployee")
public String addEmployee(Model model) {
Employee employee = new Employee();
model.addAttribute("employee", employee);
return "addNewEmployee";
}
@RequestMapping()
public String saveEmployee(@ModelAttribute("employee") Employee employee){
employeeService.saveEmployee(employee);
return "index";
}
addNewEmployee.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Add Employee</h1>
<form th:method="POST" th:action="@{shop/allEmployees}" th:object="${employee}">
<label for="name">Enter name:</label>
<input type="text" th:field="*{name}" id="name"/>
<br/>
<label for="surname">Enter surname:</label>
<input type="text" th:field="*{surname}" id="surname"/>
<br/>
<label for="department">Enter department:</label>
<input type="text" th:field="*{department}" id="department"/>
<br/>
<label for="salary">Enter salary:</label>
<input type="text" th:field="*{salary}" id="salary"/>
<br/>
<input type="submit" value="Create!">
</form>
<br><br>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
myrestController.java
中,我没有看到任何@postmapping
定义的。在addnewemployee.html
中,您似乎正在尝试使用post
而不是get> get
方法来调用Shop/Allemployees。如果您的意图是将车身或表格传递给shop/allemployees
endpoint,则可能需要考虑将@getMapping
更改为@postmapping 接受
@requestbody
或创建一个全新的@postmapping
接受@requestbody
。In your
myRestController.java
, I am not seeing any@PostMapping
defined. InaddNewEmployee.html
, it appears you are attempting to call shop/allEmployees with aPOST
rather than theGET
method. If your intention is to pass a body or form to theshop/allEmployees
endpoint, you may want to consider either changing your@GetMapping
to a@PostMapping
that accepts a@RequestBody
or creating an entirely new@PostMapping
that accepts a@RequestBody
.