开发者

Spring MVC 3.0 with Apache Tiles - Mutiple Forms in one page

I'm using spring mvc (3.0) with apache tiles in my project. I have multiple forms in a single page rendered through tiles.

The login form and search form are common to most pages. The “body” in the tile definition keeps changing.

开发者_如何学Python

So, as shown below, in all of my mvc controllers I have to explicitly set command object in the corresponding model. 1. model.put("userBO", userBO); 2. model.put("searchBO", searchBO);

Is there a way I can move this part of the code to a common place or a global controller, so that I don’t have to write these two lines in all the controllers that I write?


You can use an interceptor to do this in a postHandle:

public class DefaultModelInterceptor extends HandlerInterceptorAdapter {

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

    modelAndView.addObject("userBO", userBO);
    modelAndView.addObject("searchBO", searchBO);

    super.postHandle(request, response, handler, modelAndView);
    }
}

This can then be wired in your spring servlet config:

<mvc:interceptors>
    <bean class="my.package.DefaultModelInterceptor"/>
</mvc:interceptors>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜