Joseph
Joseph

Reputation: 11

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.

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?

Upvotes: 0

Views: 930

Answers (1)

Kieran
Kieran

Reputation: 6196

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>

Upvotes: 2

Related Questions