使用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.htmlis 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'sactionvalue), 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
- Move
results.htmlto thesrc/main/resources/templatesfolder (this is where Spring looks for template files by default, especially if you're using Thymeleaf). - Update
results.htmlto 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 incom.example.demo.controllers). - Verify filenames: Make sure
results.htmlis spelled correctly and lives insrc/main/resources/templates. - Confirm input
nameattributes match the@RequestParamvalues in the controller—case matters!
内容的提问来源于stack exchange,提问作者Jonathan Small

