简体   繁体   中英

Error: Expected session attribute 'form'

When I sent this post request I got this error:

 org.springframework.web.HttpSessionRequiredException: Expected session attribute
 'form' 
 at   org.springframework.web.method.annotation.ModelFactory.initModel

What're the causes (possibilities) for this kind of exception? I Am using Spring MVC to develop this web application.

@RequestMapping(value = "/new/form",params ="mode",method = RequestMethod.POST)
public String newForm(@ModelAttribute("form") ApplicationForm form,

                               BindingResult bindingResult,
                               @RequestParam String mode,
                               Model model,
                               SessionStatus sessionStatus) {

    return showSearchForm(model);
} 

try using these annotations at the beginning of the class

@SessionAttributes({"form"})

@Controller

This problem is caused by Spring MVC unable to find "form" in your session to be populated to your model. Typically this happen the very first time the user made request. Often the pattern used to handle this scenario is to check the existence of the model attribute, and create a new one if it doesn't exist

if(!model.containsAttribute("form")) model.addAttribute("form", new Form());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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