不支持请求方法 'POST'。 Spring+百里香叶
Request method 'POST' not supported. Spring+Thymeleaf
**大家好。我 运行 在开发 Web 应用程序时遇到了问题。 saveEmployee 方法无法正常工作并抛出相应的错误。在项目中我使用 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>
在您的 myRestController.java
中,我没有看到任何 @PostMapping
定义。在 addNewEmployee.html
中,您似乎正试图使用 POST
而不是 GET
方法调用 shop/allEmployees。如果您打算将正文或表单传递到 shop/allEmployees
端点,您可能需要考虑将 @GetMapping
更改为接受 @RequestBody
的 @PostMapping
或创建一个接受 @RequestBody
.
的全新 @PostMapping
**大家好。我 运行 在开发 Web 应用程序时遇到了问题。 saveEmployee 方法无法正常工作并抛出相应的错误。在项目中我使用 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>
在您的 myRestController.java
中,我没有看到任何 @PostMapping
定义。在 addNewEmployee.html
中,您似乎正试图使用 POST
而不是 GET
方法调用 shop/allEmployees。如果您打算将正文或表单传递到 shop/allEmployees
端点,您可能需要考虑将 @GetMapping
更改为接受 @RequestBody
的 @PostMapping
或创建一个接受 @RequestBody
.
@PostMapping