我正在使用 Spring Boot MVC 開發一個應用程式,并且我有一個登錄頁面,每當我使用 chrome 在表單上輸入資料時,我的瀏覽器不會將我重定向到我在 Controler 類中指定的頁面,而是發送一個 GET請求它應該發送 POST 請求的位置。這是我的控制器類
@Controller
@RequestMapping("/login")
public class loginController {
private final AuthService authService;
public loginController(AuthService authService) {
this.authService = authService;
}
@GetMapping
public String returnLogIn() {
return "login";
}
@PostMapping
public String login(@RequestParam String username, @RequestParam String password, Model model, HttpServletRequest request) {
User user = null;
try {
user = this.authService.login(username, password);
model.addAttribute("User", user);
request.getSession().setAttribute("user",user);
return "redirect:/home";
} catch (InvalidArgumentsException exception) {
model.addAttribute("hasError", true);
model.addAttribute("error", exception.getMessage());
return "login";
}
}
}
As you see if the login is successful then I should be redirected to the home page but It doesn't happen I get redirected to the login page again and all that changes is the URL in it the parameters I've given are appended. But when I use POSTMAN everything works just fine a POST request is sent and I get redirected to the /home page just like I've specified it in my Controller class. But I don't know why this wont happen when I use chrome. Having to use POSTMAN everytime I do a small change is really time-consuming. Also this is the HTML form
<form id="form-id" th:method="POST" th:action="@{/login}">
<div class="mb-3">
<input type="text" class="form-control" name="username" id="username"
aria-describedby="emailHelp"
placeholder="User Name">
</div>
<div class="mb-3">
<input type="password" class="form-control" name="password"
id="password" placeholder="Password">
</div>
<!-- TODO use hasError set it in the model -->
<div th:if="${hasError}">
<span class="error text-danger" th:text="${error}"></span>
</div>
<div class="text-center"><button type="submit" class="btn btn-color px-5 mb-
5 w-100 btn btn-dark" >Login</button></div>
</form>
I don't think there is something wrong with my code since everything works fine when I use POSTMAN but I really don't know why It wont work when I use my browser. Javascript is enabled in my browser I really don't know what seems to be the issue. I also tried mapping the POST request to a different URL but still I get the same issue. I really don't know what seems to be the issue
As mentioned I can't send POST request using chrome but everything works fine with POSTMAN.
GET 請求而不是 POST - 正如您所看到的那樣,GET 請求正在代替它應該是 POST 請求,在這里使用 Postman 一切正常我被重定向到主頁: Postman request 我真的不知道該怎么做提到我無法繼續從事該專案,因為我需要用戶會話我還在 gitlab 上上傳了該專案,您可以在這里找到它: https ://gitlab.com/nisizenuni/betstar
uj5u.com熱心網友回復:
在 spring 表單中使用 POST 時,您從表單傳遞的值不會作為查詢引數傳遞。當您以某種形式使用 GET 方法操作時,就會發生這種情況。
所以這就是郵遞員可以作業但您的前端不能與后端結合使用的原因。在郵遞員中,您可以在手動定義請求引數時正確發送請求,因此控制器可以正確地處理請求。但是,您的前端不會將值作為查詢引數發送,因此您的控制器無法正確處理請求。
您可以通過進行以下更改來更正此問題。
首先創建一個簡單的 DTO 物件,Spring 可以在其中系結傳入的值,這些值到達 JSON 請求的主體。
public class UserInfo {
private String userame;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
然后你可以將你的控制器定義為
@PostMapping
public String login(@ModelAttribute UserInfo userInfo, Model model, HttpServletRequest request) {
String username = userInfo.getUsername();
String password = userInfo.getPassword();
.... same as the existing controller that you have
}
同樣在前端,您需要將表單定義為
<form id="form-id" th:method="POST" th:action="@{/login}" th:object="${userInfo}">
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/449826.html
上一篇:如何使用AndroidKotlin在Fragment中使用背景關系?
下一篇:MinimalAPI-您的意思是將“正文(推斷)”引數注冊為服務還是應用[FromService]或[FromBody]屬性?
