Spring MVC- several parts/views from a controller

Posted by leeeroy on Stack Overflow See other posts from Stack Overflow or by leeeroy
Published on 2010-04-12T23:57:08Z Indexed on 2010/04/13 0:03 UTC
Read the original article Hit count: 300

Filed under:
|

i'm looking into using Spring MVC - how would I structure the fact that a page can be composed of several different views Consider a controller that's basically:

 public ModelAndView handleRequest(HttpServletRequest request, 
                                   HttpServletResponse response) 
                                  throws ServletException, IOException {
          List<Post> posts = repository.getNewPosts();

          return new ModelAndView("posts", "posts", posts);
    }

However, I decide that on every page, I'd also want a side "pane" that shows data based on some logic. That "pane" would just be a .jsp that gets included from the above "posts" view, and I could change my controller to:

 public ModelAndView handleRequest(HttpServletRequest request, 
                                   HttpServletResponse response) 
                                  throws ServletException, IOException {
      List<Post> posts = repository.getNewPosts();
      List<Items> items= repository.getItems();    
      return new ModelAndView("model", "model", new MyCommonViewModel(posts,items));
    }

But then I'd have to change every controller to also do List<Items> items= repository.getItems(); , and change everything again the next time I come up with something new.

This seems messy. How do I structure this ?

`

© Stack Overflow or respective owner

Related posts about java

Related posts about spring