Dynamic content in all page in Spring MVC

守給你的承諾、 提交于 2019-12-12 00:48:45

问题


I am developing my site in Spring MVC with Sitemesh.

Dynamic content is changed oon every page, Menu and Footer I can include in template definition. But there comes a problem. On every site below the dynamic content there should be a news list with loaded some news from my DB. I created my @Controller and it loads 5 latest news, but how to add this on my template? What request mapping should implement my news controller?


回答1:


I don't kwow how Sitemesh works, but I solved problems like that by using interceptor:

create a class that extends : HandlerInterceptorAdapter

Override the method postHandle and populate the modelAndView Object like this :

modelAndView.addObject("newslist",myNewsList);

So you will have a variable $newslist injected into all your views.

Don't forget to declare bean in your mvc-congig.xml :

<bean id="newsListInterceptor" class="mypackage.NewsListInterceptor"/>

As the interceptor is executed for each request I also use ehcache to store the result and avoid during a select in database for each call.



来源:https://stackoverflow.com/questions/15500638/dynamic-content-in-all-page-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!