Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: @SessionAttributes shared among controllers?

I have an abstract controller support class for searches and result lists:

@Controller
@SessionAttributes("query")
public abstract class SearchController<Q extends SearchQuery> {
    @RequestMapping
    public String performSearch(@ModelAttribute("query") Q query) {
        ....
    }

    @ModelAttribute("query")
    public abstract Q createDefaultSearchQuery();
}

Several actual search controllers extend this base class.

After having accessed one of the controllers (say /searchBooks.html using BookSearchQuery implements SearchQuery) the query is correctly stored in the session, available for subsequent requests.

However, when I access another controller (say /searchAuthors.html using AuthorSearchQuery implements SearchQuery) the query from the last request (BookSearchQuery) is still being used for the new controller causing a ClassCastException later on.

I have tried moving the @SessionAttribute annotation from the support class to the implementation classes, to no avail.

Is there something I'm doing wrong or is this by design? What can I do?

Thanks a lot!

like image 550
Philipp Jardas Avatar asked Oct 14 '22 22:10

Philipp Jardas


1 Answers

I just tried this on Spring 3.0.2 and the session attributes are not shared between controllers. In fact I was looking for the opposite effect and that's how I found out.

like image 161
mikebz Avatar answered Oct 18 '22 02:10

mikebz