Home > Software design >  Spring html button replies with 404 error
Spring html button replies with 404 error

Time:05-12

My problem is that the submit-button below the form should redirect the registrating user to the welcome page after pressing it.

So far, the button wants to redirect to "welcome.html" but I constantly get an error 404, which I don't understand.

The welcome-html doesn't do anything fancy, just a simple text saying hello.

My Controller looks as follows:

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 >
                <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>

[source folders for path finding]

CodePudding user response:

  1. Please add action="register" on your <form>. It is specifying where does the form go to after submitting.
  <form action="register"  modelAttribute = "form" method ="post" th:object=${form}>
  1. Remove <a href ="welcome.html">. Because your controller has redirect statement return "redirect:/welcome";, a html link is unnecessary.

It should be work as you expect.

  • Related