为什么我的浏览器在应该发送POST请求[Spring Boot]的表单上发送GET请求



我正在使用Spring Boot MVC开发一个应用程序,我有一个登录页面,每当我使用chrome在表单上输入数据时,我的浏览器不会将我重定向到我在Controler类中指定的页面,而是在应该发送POST请求的地方发送GET请求。这是我的控制器类

@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";
}
}
}

正如你所看到的,如果登录成功,我应该被重定向到主页,但没有发生这种情况,我再次被重定向到登录页面,所有的更改都是添加了其中的URL和我给出的参数。但是,当我使用POSTMAN时,一切都很好,发送POST请求,我会被重定向到/home页面,就像我在Controller类中指定的那样。但我不知道为什么当我使用chrome时不会发生这种情况。每次我做一个小改动都必须使用POSTMAN,这真的很耗时。这也是HTML表单

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

我不认为我的代码有什么问题,因为当我使用POSTMAN时一切都很好,但我真的不知道为什么当我使用浏览器时它不起作用。我的浏览器中启用了Javascript,我真的不知道问题出在哪里。我还尝试将POST请求映射到不同的URL,但仍然遇到了同样的问题。我真的不知道的问题是什么

如前所述,我不能使用chrome发送POST请求,但使用POSTMAN一切都很好。

GET请求而不是POST-正如你所看到的,GET请求应该是POST请求,在这里Postman一切都很好,我被重定向到主页:邮差请求我真的不知道该怎么做,因为我需要用户的会话,所以我不能继续做这个项目我还在gitlab上上传了这个项目。你可以在这里找到它:https://gitlab.com/nisizenuni/betstar

在spring表单中使用POST时,从表单传递的值不会作为查询参数传递。当您以某种形式使用GET方法操作时,就会发生这种情况。

所以这就是为什么poster可以工作,但您的前端不能与后端结合工作的原因。在poster中,当您手动定义请求参数时,您可以正确地发送请求,因此控制器可以正确地提供请求。但是,您的前端不会将值作为查询参数发送,因此您的控制器不能正确地为请求提供服务。

您可以通过进行以下更改来更正此问题。

首先创建一个简单的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}">

最新更新