为什么我在使用 Thymeleaf 的 spring 引导 post 请求中出现 404 错误

Why I get 404 error in spring boot post request using Thymeleaf

我用户 spring 引导创建一个积分计算器。我在尝试处理 post 请求时遇到奇怪的 404 错误。 这是我的控制器 class 应该可以处理我的请求。这是测试版所以不介意param函数不使用(如果我理解正确,这应该不会影响我的错误)。

@Controller
public class MainController {

    private final CalculatorService calculatorService;

    public MainController(WebCalculatorService calculatorService) {
        this.calculatorService = calculatorService;
    }

    @GetMapping(value = "/")
    public String index() {
        return "index";
    }

    @PostMapping(value = "/calculate")
    public String calculate(Model model, @RequestParam("function") String function, @RequestParam("numFrom") double numFrom,
                          @RequestParam("numTo") double numTo, @RequestParam("step") double step,
                          RedirectAttributes redirectAttributes) {

        double rectangleResult = calculatorService.integrateRectangleMethod(numFrom, numTo, step, x -> Math.pow(x, 2));
        double trapezoidalResult = calculatorService.integrateRectangleMethod(numFrom, numTo, step, x -> Math.pow(x, 2));

        model.addAttribute("rectangleResult", rectangleResult);
        model.addAttribute("trapezoidalResult", trapezoidalResult);

        redirectAttributes.addFlashAttribute("rectangleResult", rectangleResult);
        redirectAttributes.addFlashAttribute("trapezoidalResult", trapezoidalResult);

        return "redirect:/";
    }
}

这是我的 html 页面,使用 Thymeleaf 和 post 形式的参数来求解方程。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<header>Integral Calculator</header>

<head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
    <title>Integral Calculator</title>
    <link href="/static/main.css" th:href="@{/main.css}" rel="stylesheet"/>
</head>

<body>
<div class="container">
    <section class="container">

        <form method="POST" th:action="@{/calculate}">

            <label><span class="label-text">Enter integrand f(x): </span><input class="input-cb" type="text"
                                                                                name="function"></label>
            <fieldset>
                <legend>Enter range:</legend>
                <label><span class="label-text">From: </span><input class="input-range-from" type="number"
                                                                    name="numFrom"></label>
                <label><span class="label-text">To: </span><input class="input-range-to" type="number" name="numTo"></label>
            </fieldset>
            <label><span class="label-text">Enter step: </span><input class="input-step" type="number" value="0.01"
                                                                      step="0.1" min="0" name="step"></label>
            <p class="error-message is-hidden"></p>

            <button type="submit" name="submit" value="value" class="link-button">Calculate</button>
        </form>

        <div class="results is-hidden">
            <p class="result">Results:</p>
            <p class="result-rect">${rectangleResult}</p> <br/>
            <p class="result-trap">${trapezoidalResult}</p> <br/>

        </div>

    </section>
    <section class="chart-container">
        <p class="chart-info is-hidden">The chart is only available for real number results</p>
        <canvas id="myChart"></canvas>
    </section>
</div>
</body>

<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.0.2/math.js"></script>
</html>

在我按下我的按钮后计算出一个 get 404 错误,这就是它的样子 enter image description here

路径如下:http://localhost:8080/calculate

我已经尝试过更改路径,在 html 页面中更改我的表单,但没有帮助,老实说我不明白我这里的错误,请提前帮助并感谢

我解决了我的问题,只是重新编译了我的项目,然后这个愚蠢的错误就消失了。不过谢谢大家的回答。