5

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!

Philipp Jardas
  • 3,222
  • 3
  • 29
  • 42

2 Answers2

4

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.

mikebz
  • 3,277
  • 8
  • 37
  • 50
0

Using @SessionAttributes this objects will be stored in your HttpSession with the same name and they will be accessible (shared) from different controllers. So, Spring is acting correctly.

I think the best approach in your scenario is rename this attribute in every subclass ("BookQuery", "AutorQuery", ...). Not very elegant at all :(

sinuhepop
  • 20,010
  • 17
  • 72
  • 107
  • I had hoped Spring would somehow qualify the name of the attribute in the session context with the controller name. I can't rename the attribute name because I'm using the model bean name ("query") in the methods of the abstract controller. Any ideas? – Philipp Jardas Oct 26 '10 at 16:37
  • Sorry, no ideas, and I think what you want is not possible. – sinuhepop Oct 27 '10 at 09:44