Spring MVC所有页面中的动态内容 [英] Dynamic content in all page in Spring MVC
问题描述
我正在使用Sitemesh在Spring MVC中开发我的网站.
I am developing my site in Spring MVC with Sitemesh.
动态内容会在每页上更改,我可以在模板定义中包含菜单和页脚.但是有一个问题.在动态内容下方的每个站点上,都应该有一个新闻列表,其中载有来自我的数据库的一些新闻.我创建了@Controller并加载了5条最新消息,但是如何在我的模板上添加它呢?什么请求映射应实现我的新闻控制器?
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?
推荐答案
我不知道Sitemesh的工作原理,但是我使用拦截器解决了类似的问题:
I don't kwow how Sitemesh works, but I solved problems like that by using interceptor:
创建一个扩展类: HandlerInterceptorAdapter
覆盖方法 postHandle ,然后像这样填充modelAndView对象:
Override the method postHandle and populate the modelAndView Object like this :
modelAndView.addObject("newslist",myNewsList);
因此,您将在所有视图中插入变量 $ newslist .
So you will have a variable $newslist injected into all your views.
别忘了在您的mvc-congig.xml中声明bean:
Don't forget to declare bean in your mvc-congig.xml :
<bean id="newsListInterceptor" class="mypackage.NewsListInterceptor"/>
在为每个请求执行拦截器时,我还使用ehcache来存储结果,并避免在每次调用时在数据库中进行选择.
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.
这篇关于Spring MVC所有页面中的动态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!