【发布时间】:2021-12-14 01:46:04
【问题描述】:
我在 SpringBoot 项目中有一个 Controller 类来提供登录功能。当我输入用户名和密码并按提交时,它会使用 method="POST" 调用 LoginController。我希望当我使用模型添加属性“客户”时,“客户”属性也将通过 @SessionAttributes(“客户”) 添加到会话中。相反,它返回错误
这是错误
Expected session attribute 'customer'
org.springframework.web.HttpSessionRequiredException: Expected session attribute 'customer'
这是我的代码
@Controller
@SessionAttributes("customer")
public class LoginController {
@Autowired
CustomerService customService;
@RequestMapping(method=RequestMethod.GET,value="/login")
public String login() {
return "login";
}
@RequestMapping(method=RequestMethod.POST,value="/login")
public String submitLoginForm(@ModelAttribute Customer customer, Model model) {
model.addAttribute("customer", customService.getCustomer(customer.getUsername(), customer.getPassword()));
return "redirect:/";
}
}```
【问题讨论】:
标签: java spring-boot