登陆页面的controller层commonResult
在登录页面的Controller层中,可以使用CommonResult类来返回登录结果。CommonResult类是一个自定义的响应结果对象,可以包含登录成功或失败的信息。以下是一个示例代码:
@RestController
@RequestMapping("/login")
public class LoginController {
@Autowired
private UserService userService;
@PostMapping
public CommonResult login(@RequestParam String username, @RequestParam String password) {
// 调用userService的登录方法进行验证
boolean isAuthenticated = userService.authenticate(username, password);
if (isAuthenticated) {
// 登录成功
return new CommonResult(true, "登录成功");
} else {
// 登录失败
return new CommonResult(false, "用户名或密码错误");
}
}
}
在上述代码中,login方法接收用户名和密码作为参数,并调用userService的authenticate方法进行验证。如果验证成功,则返回一个CommonResult对象,其中success字段为true,message字段为"登录成功";如果验证失败,则返回success字段为false,message字段为"用户名或密码错误"。最终,Controller层就可以根据这个结果来进行相应的处理,例如返回给前端一个JSON对象表示登录结果。
原文地址: https://www.cveoy.top/t/topic/i3A8 著作权归作者所有。请勿转载和采集!