Spring MVC - several parts / views from the controller

I am considering using Spring MVC - how would I structure the fact that a page can consist of several different views? Consider a controller, which 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 also need a third-party panel that displays data based on some logic. This β€œpanel” will simply be β€œ.jsp”, which will be included in the above β€œposts” view, and I can 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 would have to change each controller to do List<Items> items= repository.getItems(); , and again, change everything the next time I come up with something new.

It seems dirty. How to structure it?

`

+4
source share
4 answers

Take a look at Tiles .

+3
source

If each page of your system should display a panel view, then you can provide model data for this using an interceptor . It is almost always preferable to inject code into an abstract controller (as suggested in another answer) because it forces you to always extend this from this particular class hierarchy.

 public class YourInterceptor extends HandlerInterceptorAdapter { private Repository repository; public void setRepository (Repository repository) { this.repository = repository; } public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { List<Post> posts = repository.getNewPosts(); List<Items> items= repository.getItems(); request.setAttribute("model", new MyCommonViewModel(posts,items)); } } 

connect it like

 <beans> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="yourInterceptor"/> </list> </property> <property name="mappings"> <props> <!-- SNIP --> </props> </property> </bean> <bean id="yourInterceptor" class="your.YourInterceptor"> <property name="repository"><ref bean="repository" /></property> </bean> <beans> 

You can also look at using sitemesh . This allows you to create a template page, which is the basis for all of your other pages (or specific sets of pages depending on how you configure it). This will prevent you from turning on the jsp panel on every page. But if your project is small enough, this may not be worth the bother.

+3
source

First, you can extend AbstractController so that you have a base class that can load the data that you need to use in all your controllers.

 public class AbstractHasPaneController extends AbstractController { public Object getPaneData() { return repository.getItems(); } // ... } 

Then your other controllers expand your abstract controller

 public class MyPageController extends AbstractHasPaneController { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ModelAndView modelAndView = new ModelAndView("posts"); modelAndView.addObject( "posts", repository.getNewPosts() ); modelAndView.addObject( "model", this.getPaneData() ); return modelAndView ; } // ... } 

When you come up with something new, all you need to change is AbstractHasPaneController.getPaneData ().

0
source

I very much predetermine the proposal to use HandlerInterceptor to provide general information about your view. And to extend the response from the bart and ease the configuration of interceptors, Spring 3.0 provides the mvc configuration namespace

In the top level element beans just add

 xmlns:mvc="http://www.springframework.org/schema/mvc" 

And inside your bean configuration you can use:

 <mvc:annotation-driven /> <mvc:interceptors> <bean class="org.example.web.interceptors.YourInterceptor"/> </mvc:interceptors> 
0
source

Source: https://habr.com/ru/post/1306749/


All Articles