Skip to content

CM7: Building links with the CAE

by on October 19, 2012

The new CoreMedia CMS release “CM7” is just around the corner. Besides lots of new functionality it consists of improvements that leverages the application developer’s experience and thus make his life easier. Today I want to introduce the Content Application Engine’s (CAE) new way of generating links.

First of all, a short recap: When introducing the Content Application Engine (CAE) in 2005 a concept called “link schemes” has been introduced as well. A link scheme is a piece of java code that generates links (or URIs respectively). It can be used from witin JSP templates for building links on other resources that are represented as beans. This concept can be understood as a counterpart of the Spring Framework’s way of handling requests using controllers or handlers: While a handler/controller is responsible for translating an URI into beans and wrap them in ModelAndView object, it’s the link scheme’s responsibility to translate a bean to an URI that typically points back to a handler again.

The advantage of this concept is that a JSP template programmer doesn’t need to care about the concrete characteristic of a link (which might be also quite error-prone) but can still remain on the more abstract bean level. As a consequence it is for example possible to change the link format without touching hundreds of JSP templates.

Example

Instead of writing JSP/JSTL code like

 <a href="/page/<c:out value='${nextPage.uriText}'/>/<c:out value='${nextPage.id}'/>">next</a>

the programmer might instead use the link tag

<a href="<cm:link target='${nextPage}'/>">next</a>

that delegate the link generation to the link scheme(s).

Links and Handlers in CM7

One of CM7’s improvements for the CAE is the better support for Spring MVC’s (version 3.1) annotation driven request handling, that prevents writing lots of boiler plate code when implementing a handler.

Example

@RequestMapping(value="/page/{uriText}/{id}")
public ModelAndView renderPage(@PathVariable("id") Integer id) {

  Page page = getPageContent(id);
  return new ModelAndView(null, "self", page);
}

This examples demonstrates a handler that listens to request URIs like “/page/text/id” that doesn’t need to deal with extraction and conversion of URI segments.

In addition, a new annotation driven way for implementing link schemes has been introduced to the CAE in CM7 as well. The easiest way to demonstrate this is by example again:

@Link(type=Page.class, uri="/page/{uriText}/{id}")
public UriComponents buildPageLink(Page linkedPage, UriComponentsBuilder uriTemplate) {

  Map<String, Object> uriVariables = new HashMap<String, Object>();
  uriVariables.put("uriText", linkedPage.getUriText());
  uriVariables.put("id", linkedPage.getId());
  return uriTemplate.buildAndExpand(uriVariables);
}

By annotating a service’s method with @Link, this method is turned into a link scheme. The attribute type is used for indicating for which types of beans this scheme shall be invoked. Whenever a link shall be generated, it’s the framework’s responsibility to choose the best matching link scheme according to information like this. The attribute uri holds an URI template that is automatically passed to the scheme’s method as a org.springframework.web.util.UriComponentsBuilder parameter. The only remaining thing to implement for this scheme is to build the link from the URI template by assigning values to the URI template variables (id and uriText) to concrete values.

In this example, it is assumed that the variable values are managed the bean itself. As a consequence, the link scheme’s code becomes quite lean while the business code remains in adequate business objects. Necessary internal conversions such as a proper URI encoding are performed by the framework and doesn’t need to be considered by the implementation.
Note that the the link scheme method could be added to the same class that holds the corresponding handler method. This pattern is useful in cases when link scheme and handler are related,e.g. when dealing with the same kind of resources and when using the same URI templates.

Conclusion: With CM7 and Spring MVC 3.1 it becomes easier to implement CAE based web applications. Developers can better focus on writing business code rather than writing reoccuring boiler plate code.

From → Dev

One Comment
  1. winni permalink

    Great to see a more CoreMedia project relevant dev post now. Makes me curious what other highlights for CM7 are in the box.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: