MVCC

Description

Model, View and Controller Components (MVCC) is a component extension for Spring MVC. It allows one to use Spring controllers as controllers/views embedded in other controllers/views. Simplest use case for MVCC is a replacement for SiteMesh providing common header, navigation and footer for content pages. More advanced use cases include components that require interaction with user (e.g. table with parametrized paging and sorting). Typically this kind of "components" encapsulate rendering as well as request parameter handling in a layout component (e.g. JSP Tag) thus violating MVC's basic principle, that a controller should handle all user input and provide a model for a view to render. On the other hand handling component specific inputs in controllers results usually in a lot of redundant code as a component's parameters' need to be handled by all controllers using the component.

MVCC allows encapsulating common functionality implemented using MVC as components to pages. As these components are themselves controllers and parametrized mainly using HTTP-parameters they can also be used directly as main request handlers. Thus the same stateless component can also be used e.g. in Ajax requests.

Example

public class LayoutController implements Controller {

    private ComponentRequestAdapter componentAdapter = new ComponentRequestAdapter();

    private HandlerMapping handlerMapping;

    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        Object handler = handlerMapping.getHandler(request).getHandler();

        ComponentModelAndView cmv = componentAdapter.createBuilder(response)
            .handler(handler)
            // additional http-request parameters for component:
            // .parameter(name, value) 
            .handleRequest();

        if (cmv.isDispatcherView()) {
            // Redirect...
            return cmv;
        } else {
            return new ModelAndView("layout", "contents", cmv);
        }
    }
}

And in layout.ftl:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <title>Application Title</title>
</head>
<body>
<div> Header </div>
<div> Navigation </div>
<div>
  <@renderComponent cmv=contents/>
</div>
<div> Footer </div>
</body>
</html>

Component Parameters

MVC Components should get their parameters as HTTP-request parameters. Request attributes may also be used but those are not usable in e.g. direct Ajax requests. When using multiple components on a single page, there is a change that those may use same parameter names for different purposes. MVCC resolves this by letting the user specify component ID that is used as a prefix for given component's parameters:

ComponentModelAndView cmv = componentAdapter.createBuilder(response)
    .handler(handler)
    .contextId("contents")
    .handleRequest();

Now the given handler only sees HTTP-parameters that start with "contents/". Other than for contextId of component request, controllers can mostly ignore this prefix. They get their parameters directly without any prefix set nested component request parameters alike. In templates this needs to be taken into account by prefixing component's own parameters with parameterContext:

<@spring.bind path />
<input type="text" 
    name="${parameterContext + spring.status.expression}"
    value="${spring.status.value!}"/> 

Bookmark with: del.icio.us   digg   Mister Wong   YahooMyWeb   Reddit   Furl   Spurl   blogmarks

In a nutshell

Name MVCC
Most Recent Version 0.1.0
Javadocs v 0.1.0
State Stable
License LGPL v2.1
Version Control on Mysema Source
Maven repo on Mysema Source
Issue Management on Launchpad

Labels

spring spring Delete
mvcc mvcc Delete
mvc mvc Delete
web web Delete
ui ui Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.