You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Java与Thymeleaf开发表单提交遇404错误求助

Hey Jonathan, let's break down why you're hitting that 404 error and fix your form submission issue step by step!

First, Let's Diagnose the Core Problem

That 404 happens because either:

  • Your form is submitting to a path that has no corresponding handler in Spring, or
  • Your results.html is stored in a location Spring can't automatically serve, and you're not routing to it properly.
    The URL changing after submission is normal (it reflects the form's action value), but we need to make sure that action points to a valid endpoint.

Step 1: Fix Your index.html Form Code

First, update your form to use a POST request (more secure for form data) and point it to a Spring controller endpoint instead of directly to results.html. Here's an example:

<!DOCTYPE html>
<html>
<head>
    <title>Input Form</title>
</head>
<body>
    <h1>Enter Your Details</h1>
    <!-- Point action to a controller endpoint, not directly to results.html -->
    <form method="POST" action="/submit-form">
        <label for="field1">Field 1:</label>
        <input type="text" id="field1" name="field1" required><br><br>
        
        <label for="field2">Field 2:</label>
        <input type="text" id="field2" name="field2" required><br><br>
        
        <label for="field3">Field 3:</label>
        <input type="text" id="field3" name="field3" required><br><br>
        
        <button type="submit">Submit</button>
    </form>
</body>
</html>

Make sure the name attributes on your inputs match the parameter names we'll use in the controller later.


Step 2: Create a Spring Controller to Handle the Submission

You need a controller to catch the form submission, grab the input values, and pass them to results.html. Create a class like this in your project (make sure it's in a subpackage of your main Spring Boot application class, so Spring can scan it):

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class FormSubmissionController {

    // Map this method to the form's action path
    @PostMapping("/submit-form")
    public String handleFormSubmit(
            // Grab values from the form using their "name" attributes
            @RequestParam("field1") String field1,
            @RequestParam("field2") String field2,
            @RequestParam("field3") String field3,
            // Model lets us pass data to the view (results.html)
            Model model) {
        
        // Add the input values to the model
        model.addAttribute("field1Value", field1);
        model.addAttribute("field2Value", field2);
        model.addAttribute("field3Value", field3);
        
        // Return the name of the view file (without .html)
        // Spring will look for this in src/main/resources/templates/
        return "results";
    }
}

Step 3: Set Up results.html Correctly

  1. Move results.html to the src/main/resources/templates folder (this is where Spring looks for template files by default, especially if you're using Thymeleaf).
  2. Update results.html to display the values passed from the controller using Thymeleaf syntax (you'll need Thymeleaf dependency added first—see Step 4):
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Submission Results</title>
</head>
<body>
    <h1>Your Submitted Values</h1>
    <p>Field 1: <span th:text="${field1Value}"></span></p>
    <p>Field 2: <span th:text="${field2Value}"></span></p>
    <p>Field 3: <span th:text="${field3Value}"></span></p>
</body>
</html>

Step 4: Add Thymeleaf Dependency (If Missing)

To use the template rendering and data passing features, add the Thymeleaf starter dependency to your pom.xml (Maven) or build.gradle (Gradle):

Maven (pom.xml):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Gradle (build.gradle):

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

Quick Checks If You Still Get 404

  • Double-check your controller's package path: It should be a subpackage of your main Spring Boot application class (e.g., if your app class is in com.example.demo, the controller should be in com.example.demo.controllers).
  • Verify filenames: Make sure results.html is spelled correctly and lives in src/main/resources/templates.
  • Confirm input name attributes match the @RequestParam values in the controller—case matters!

内容的提问来源于stack exchange,提问作者Jonathan Small

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:15:37