请求方法' post'不支持。弹簧胸骨

发布于 2025-01-27 14:31:21 字数 2377 浏览 2 评论 0原文

**大家好。在开发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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2025-02-03 14:31:21

在您的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. In addNewEmployee.html, it appears you are attempting to call shop/allEmployees with a POST rather than the GET method. If your intention is to pass a body or form to the shop/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文