Spring MVC复杂模型来自多个来源 [英] Spring MVC complex model population from multiple sources

查看:158
本文介绍了Spring MVC复杂模型来自多个来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我的问题可能听起来有点模糊,但无论如何它在这里。
我正在使用Spring MVC 3.1.M1,JSP 2.1构建一个Web应用程序(没有Tiles,我使用普通的JSP标记文件来构建我的布局)。



基本上,我的页面是使用一些常见部分的布局构建的 - 页眉,页脚,横幅,菜单等。这些部分大多是动态的,即包含当前用户的相关信息。



< JSP没有组件概念,所以我无法在一个地方定义我的模板的一部分及其支持java代码,耦合在一起。在我的@Controllers中,我必须完全填充我的模型,包括页眉,页脚,菜单和其他内容的数据。我真正想要做的是避免这种代码重复。具有一些通用模型填充方法的抽象BaseController类也看起来不太好。



JSP和Spring MVC经常一起使用,所以我希望存在一些最佳实践关于这个问题。
让我们讨论这个。

解决方案

springframework包含处理程序拦截器作为处理程序映射机制的一部分。

在拦截器中,您可以在执行实际处理程序之前使用 postHandle 方法。



这样的拦截器必须实现 org.springframework.web.servlet.HandlerInterceptor org.springframework.web.servlet.handler.HandlerInterceptorAdapter 用于简化实现。

 公共类MyHandlerInterceptor扩展HandlerInterceptorAdapter {

public void postHandle(HttpServletRequest request,
HttpServletResponse响应,对象处理程序,ModelAndView modelAndView)抛出异常{

//填充标题,菜单,页脚,...模型
}
}

以及处理程序映射的配置。

 < bean class =或g.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping> 
< property name =interceptors>
< list>
< bean id =myInterceptorclass =... MyHandlerInterceptor/>
< / list>
< / property>


Well, my question may sound a little bit fuzzy, but here it is, anyways. I'm building a web application using Spring MVC 3.1.M1, JSP 2.1 (without Tiles, I use plain JSP tag files to compose my layouts).

Basically, my pages are built using layouts of some common parts - header, footer, banner, menu etc. Most of these parts are dynamic, i.e. contain current user's related information.

JSP does not have a "component" notion, so I cannot define part of my template and its backing java code in some one place, coupled together. In my @Controllers, I have to fully populate my model, including data for header, footer, menu and other stuff. What I really want to do is to avoid this code duplication. Abstract BaseController class with some generic model population methods does not look good too.

JSP and Spring MVC are a very often used together, so I expect some best-practices to exist on this subject. Lets discuss this.

解决方案

The springframework contains handler interceptors as part of the handler mapping mechanism.
Within the interceptor you can use the postHandle method before the actual handler is executed.

Such a interceptor must implement the org.springframework.web.servlet.HandlerInterceptor or the org.springframework.web.servlet.handler.HandlerInterceptorAdapter for simplified implementation.

public class MyHandlerInterceptor extends HandlerInterceptorAdapter {

    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {

        //populate header, menu, footer, ... model
    }
}

and the configuration for the handler mapping.

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
    <list>
        <bean id="myInterceptor" class="...MyHandlerInterceptor"/>
    </list>
</property>

这篇关于Spring MVC复杂模型来自多个来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆