简体   繁体   English

Spring MVC @SessionAttribute Missing session 类型 String[] 属性错误

[英]Spring MVC @SessionAttribute Missing session attribute of type String[] error

I was having some problem when trying to use @SessionAttribute in Spring MVC Controller.我在尝试在 Spring MVC Controller 中使用 @SessionAttribute 时遇到了一些问题。 Basically what I am trying to do is, in my API, I want to retrieve from the session, if it contains data, fetch it out, then remove the session itself.基本上我想做的是,在我的 API 中,我想从 session 中检索,如果它包含数据,将其取出,然后删除 Z21D6F40CFB511982E4424E0E250A9557 本身。 Here is my controller class:这是我的 controller class:

@SessionAttributes({WebKeys.SEARCH_RESULT_LIST_THREE})
public class ALController {
    @RequestMapping(value = "/search.do", method = { RequestMethod.POST })
    public String doSearchList(Model model, @ModelAttribute("attendanceTO") AttendanceTO attendanceSearchForm, @SessionAttribute(WebKeys.SEARCH_RESULT_LIST_THREE) String[] selectedRecords) {
        // removed code

        attendanceSearchForm.setSelectedRecords(null);
        // checking from session
        if(selectedRecords != null && selectedRecords.length > 0){
            attendanceSearchForm.setSelectedRecords(selectedRecords);
        }
        model.addAttribute("selectedRecords", selectedRecords);
        // remove session
        model.addAttribute(WebKeys.SEARCH_RESULT_LIST_THREE, null);
    }
}

At this point of time, the session is not existed yet.此时,session 还不存在。 I only set up the session when user submit form.我只在用户提交表单时设置了 session。 I am getting this error messages:我收到此错误消息:

Missing session attribute 'key.searchResultList.three' of type String[]

Any ideas on how to resolve this?关于如何解决这个问题的任何想法? Thanks!谢谢!

U should add a required parameter to the @SessionAttribute annotation and set its value to false,meaning it's not necessary,like你应该在@SessionAttribute注解中添加一个required的参数并将其值设置为false,这意味着它不是必需的,比如

@SessionAttribute(name=WebKeys.SEARCH_RESULT_LIST_THREE,required=false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM