【发布时间】:2022-01-17 20:07:30
【问题描述】:
我有一个具有登录功能的控制器类。当我输入用户名和密码并按提交时,它将调用此控制器并将客户信息存储在会话中。但让我感到困惑的一件事是@ModelAttribute
@Controller
@SessionAttributes("customer")
public class LoginController {
@Autowired
CustomerService customService;
@ModelAttribute("customer")
public Customer getCustomer(@ModelAttribute Customer customer) {
Customer c = customService.getCustomer(customer.getUsername(), customer.getPassword());
return c;
}
@RequestMapping(method=RequestMethod.POST,value="/login")
public String submitLoginForm( Model model) {
return "redirect:/";
}
}```
我将使用@ModelAttribute Customer 客户来存储我输入的用户名和密码,并使用 Customer c 来存储我从 customService 获得的所有信息并将其存储到会话中。但会话存储的是 Customer 客户。
如果我这样改变论点。正常工作
public Customer getCustomer(@RequestParam String username, @RequestParam String password) {
Customer c = customService.getCustomer(username, password);
return c;
}
【问题讨论】:
-
@FahimazazBhanej 我正在开发一个 Spring Boot 项目。上面的控制器将负责登录功能。我将在登录页面输入用户名和密码。如果登录成功,索引页面将显示客户的全名
标签: java spring-mvc