A Sdi A Sdi - 7 days ago 6
Java Question

How to remove the session attributes in spring mvc

I am trying to deploy a Spring mvc application. I have wizard like form which is filled by user and I retain the content of each form in the wizard by utilizing session, until the last page and then user confirms and content is saved. Then the controller redirect to another page. But here I have a form with all previous session attributes, all presented in the form. My question is is there any method to remove all the session attributes from a controller before redirecting to another page
The review page controller:

@RequestMapping("/review")
public String review(@ModelAttribute Resource resource){
// do somthing
return "course_review";
}


and save course controller

@RequestMapping("/save")
public String save(@ModelAttribute Resource resource) {
// do somthing
return "redirect:/course/add";
}


and save contoler redirect to
/course/add
which is controlled by

@RequestMapping("/add")
public String add(Model model) {
//do somthing
return "course_add";
}


The Controller class is annotated with

@Controller
@RequestMapping("/course")
@SessionAttributes("course")


Please advise.

Answer

Look into this helper class.

From the docs

... attributes will be removed once the handler indicates completion of its conversational session.

@RequestMapping("/save")
public String save(@ModelAttribute Resource resource, SessionStatus status) {
    status.setComplete();
}