登陆页面的controller层
登陆页面的controller层主要负责处理用户登陆的逻辑和业务,包括验证用户输入的账号密码是否正确,根据验证结果进行相应的处理。
以下是一个示例的登陆页面的controller层代码:
@Controller
public class LoginController {
@Autowired
private UserService userService;
@GetMapping("/login")
public String showLoginPage(Model model) {
model.addAttribute("user", new User());
return "login";
}
@PostMapping("/login")
public String processLogin(@ModelAttribute("user") User user, Model model) {
// 验证用户输入的账号密码是否正确
if (userService.authenticate(user.getUsername(), user.getPassword())) {
// 登陆成功,跳转到首页
return "redirect:/home";
} else {
// 登陆失败,返回登陆页面,并显示错误消息
model.addAttribute("error", "Invalid username or password");
return "login";
}
}
}
上述代码中,showLoginPage()方法用于显示登陆页面,processLogin()方法用于处理用户提交的登陆表单。在processLogin()方法中,首先调用userService.authenticate()方法验证用户输入的账号密码是否正确,如果验证成功,则重定向到首页;如果验证失败,则返回登陆页面,并在页面上显示错误消息。
需要注意的是,上述代码中使用了@ModelAttribute注解将用户输入的账号密码绑定到User对象中,以便在处理登陆逻辑时可以直接访问用户输入的数据。同时,UserService是一个自定义的用户服务类,负责处理用户相关的业务逻辑,例如验证用户账号密码的正确性。
原文地址: https://www.cveoy.top/t/topic/i3A3 著作权归作者所有。请勿转载和采集!