簡體   English   中英

使用 Spring Boot 和 Thymleaf 發布表單錯誤地使用 GET 方法而不是 POST

[英]Using Posting a form with Spring Boot and Thymleaf incorrectly uses GET method instead of POST

我正在學習 Spring Boot 並嘗試實現身份驗證。 我有一個注冊頁面,它使用 POST 方法發送新的用戶數據以存儲在內存數據庫中。 正在發生的奇怪事情是,當我提交表單時,GET 方法是唯一被調用的方法。 這是我的 HTML 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Signup</title>
</head>
<body>
    <div class="container justify-content-center w-25 p-3" style="background-color: #eeeeee; margin-top: 5em;">
        <div class="form-group">
            <label><a th:href="@{/login}">Back to Login</a></label>
        </div>

        <h1 class="display-5">Sign Up</h1>
        <form action="#" th:action="@{/signup}" methond="POST">
            <div id="success-msg" th:if="${signupSuccess}">
                You successfully signed up! Please continue to the <a id="login-ling" th:href="@{/login}">login</a> page.
            </div>
            <div if="error-msg" th:if="${signupError}">
                <span th:text="${signupError}"></span>
            </div>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="inputFirstName">First Name</label>
                    <input type="input" name="firstName" class="form-control" id="inputFirstName" placeholder="Enter First Name" maxlength="20" required>
                </div>
                <div class="form-group col-md-6">
                    <label for="inputLastName">Last Name</label>
                    <input type="input" name="lastName" class="form-control" id="inputLastName" placeholder="Enter Last Name" maxlength="20" required>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="inputUsername">Username</label>
                    <input type="input" name="username" class="form-control" id="inputUsername" placeholder="Enter Username" maxlength="20" required>
                </div>
                <div class="form-group col-md-6">
                    <label for="inputPassword">Password</label>
                    <input type="password" name="password" class="form-control" id="inputPassword" placeholder="Enter Password" maxlength="20" required>
                </div>
            </div>
            <button id="submit-button" type="submit" class="btn btn-primary">Sign Up</button>
        </form>
    </div>
</body>
</html>

如您所見,我在表單上有method="POST"屬性。

這是我的注冊 controller:

import com.udacity.mvc.Basics.models.User;
import com.udacity.mvc.Basics.services.UserService;
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;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller()
@RequestMapping("/signup")
public class SignupController {

    private final UserService userService;

    public SignupController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping()
    public String signupView() {
        System.out.println("Get");
        return "signup";
    }

    @PostMapping()
    public String signupUser(@ModelAttribute User user, Model model) {
        String signupError = null;
        System.out.println("Post");
        if (!userService.usUsernameAvailable(user.getUsername())) {
            signupError = "The username already exist.";
        }

        if (signupError == null) {
            int rowsAdded = userService.createUser(user);
            if(rowsAdded < 0) {
                signupError = "There was an error signing you up. Please try again";
            }
        }

        if (signupError == null) {
            model.addAttribute("signupSuccess", true);
        } else {
            model.addAttribute("signupError", signupError);
        }

        return"signup";
    }
}

我已將System.out.println("GET")放在 @GetMapping() 方法中,並且可以在我的服務器上看到該方法被調用,即使我點擊了注冊表單上的提交按鈕。 有誰知道為什么會這樣?

在 html 文件中,“method”關鍵字中存在拼寫錯誤將該行替換為:

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

去掉action="#",就可以了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM