Spring html 按钮回复 404 错误

Spring html button replies with 404 error

我的问题是表单下方的提交按钮在按下后应将注册用户重定向到欢迎页面。

到目前为止,按钮想要重定向到“welcome.html”,但我不断收到错误 404,我不明白。

欢迎-html 没有做任何花哨的事情,只是一个简单的文字问候。

我的控制器如下所示:

package user;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.PostMapping;

@Controller 
public class UserController {
    @Autowired private  UserRepository users;
    @Autowired private  UserManagement UserManager;
        
    
    @GetMapping("/")
    public String register(Model model, RegistrationForm form) {
        System.out.println("Homepage");
        model.addAttribute("form", form);
    return "register";
    }
    
    @PostMapping("/register")
    public String submit(@ModelAttribute("user")User user, RegistrationForm form,
                        Model model) {
        User newUser = UserManager.createUser(form);
        model.addAttribute("name", newUser.getAccount().getUsername());
        model.addAttribute("street", newUser.getStreet());
        model.addAttribute("PLZ", newUser.getPLZ());
        model.addAttribute("mail", newUser.getMail());
        model.addAttribute("number", newUser.getPhoneNumber());
        users.save(newUser);
    return "redirect:/welcome";
    }
}
`

the register-page functions alright and the button too:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
    <html  xmlns:th="http://www.thymeleaf.org">
        <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
            <link rel="stylesheet" type="text/css" th:href="@{/resources/css/style.css}" href="../static/resources/css/style.css"/>
            <title>register</title>
        </head>
        <body>
        <header id ="mainHeader">
        <div class ="container">
                <h1>ClownCollege</h1>
        </div>
        </header>
        
        
        <nav id="mainNav">
            <div class="container">
                <ul>
                    <li><a href="">Startseite</a>
                    <li><a href="">Angebote</a>
                    <li><a href="">Login</a>
                </ul>
            </div>
        </nav>
        
        <div id="register">
            <h2>Registrierung</h2>
                <form  modelAttribute = "form" method ="post" th:object=${form}>
                <input type="text" field ="*{name}" />
                <input type="text" field ="*{street}"/>
                <input type="text" field ="*{PLZ}"/>
                <input type="text" field ="*{mail}"/>
                <input type="text" field ="*{number}"/>
                <input type="text" field ="*{password}"/>
                </form>         
                <a href ="welcome.html"> <button type ="submit">registrieren</button></a>
              
        </div>
        </body>
    </html>

[路径查找的源文件夹]

  1. 请在您的 <form> 上添加 action="register"。它指定表单提交后去哪里。
  <form action="register"  modelAttribute = "form" method ="post" th:object=${form}>
  1. 删除<a href ="welcome.html">。因为您的控制器有重定向语句 return "redirect:/welcome";,所以不需要 html link。

它应该如您所愿。