loading table of contents...

4.3.2. Building Links

It has been already stated above that handlers are responsible for providing a model object named "self" that represents a page (or another resource). This page might be rendered as HTML or another output format. A typical page consists of links pointing to other pages that are handled by a handler again when requested by the client.

Processing chain of handlers and link schemes

Figure 4.6. Processing chain of handlers and link schemes


In a CAE, links can be represented as model objects that can be translated into a URI by view technology specific mechanisms such as a JSP tag (<cm:link target="${linkRepresentation}"/>) or a Freemarker function (<#assign imageSrc=cm.getLink(self.thumbnail)!""/>). Typically, the bean that is used for building the link is the same that is provided by the handler as a model. In the CAE there is a concept called "link scheme" that is used for translating an object (with an optional view name) into a URI string. A link scheme is logically bound to a handler that is able to translate the URI back to a model. Link schemes are automatically collected by the CAE and exposed to the view technology specific link building facilities mentioned above.

Links created by a "@Link" link scheme are always relative to the servlet path. For adding servlet and context path, or making links absolute, instances of LinkPostProcessor should be used. (see below)

Example

package com.mycompany;

...
import java.net.URI;
import com.coremedia.objectserver.beans.ContentBean;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import com.coremedia.cap.common.IdHelper;

@Link
public class MyLinkScheme {

  @Link(type = ContentBean.class, uri="/content/{id}")
  public UriComponents buildLink(UriComponentsBuilder uriTemplate,
                                 ContentBean bean) {

    Integer id = IdHelper.parseContentId(bean.getContent().getId());
    return uriTemplate.buildAndExpand(id);
  }
}

Example 4.6. A link scheme


<beans xmlns="http://www.springframework.org/schema/beans">
  <import resource="classpath:/com/coremedia/cae/link-services.xml"/>
  <bean id="myLinkScheme" class="com.mycompany.MyLinkScheme"/>
</beans>

Example 4.7. Defining a link scheme


This example demonstrates how to build links that point to the above mentioned handler. This link scheme is invoked for beans of type ContentBean only and uses the same URI pattern /content/{id} that is also in use by the handler. The link is generated by simply applying the value of the path variable id to the URI template.