【问题标题】:Spring Boot @SessionAttributes not workingSpring Boot @SessionAttributes 不起作用
【发布时间】: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


    【解决方案1】:

    您必须使用 @ModelAttribute("customer") 注释 getCustomer 方法

    例如:

    @Service
    public class CustomerService {
    
        @ModelAttribute("customer")
        public Customer getCustomer(String username, String password) {
            return new Customer(username, password);
        }
    
    }
    

    模型对象

    public class Customer {
    
        private String username;
        private String password;
    
        public Customer(String username, String password) {
            this.username = username;
            this.password = password;
        }
    
        public String getUsername() {
            return username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
    }
    

    控制器

    @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(Customer customer, Model model) {            
            model.addAttribute("customer", customService.getCustomer(customer.getUsername(), customer.getPassword()));
            return "redirect:/";
        }
         
    }
    

    【讨论】:

    • Service 类中的 getCustomer 方法不起作用。但是当我使用 @ModelAttribute 将 getCustomer 方法放入 Controller 时,它可以正常工作。
    • 我认为您仍然在 submutLoginForm 方法中将 @ModelAttribute 与 Customer 一起使用。尝试使用上面提到的代码。
    猜你喜欢
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    相关资源
    最近更新 更多