This section illustrates the process of defining a data view configuration. For this example, a simple site with three pages is used. The first page consists of a brief overview of two articles that are completely shown on two separate pages. These article instances are shared between the overview page and the detail pages:
The entities are represented as beans and properties where the properties are assumed to have different costs: Some are expensive to compute while others are cheap.
Bean | Property | Description | Expensive |
---|---|---|---|
PageBean
|
Title
| The page's title. | No |
Content
|
The page content as a linked
OverviewBean
or ArticleBean .
| No | |
Navigation
|
All
PageBeans
to be rendered as navigation.
| Yes | |
ContentBean
|
Page
|
The
PageBean
which embeds this bean.
| Yes |
OverviewBean
|
Teasers
|
A list of
ArticleBeans
to be rendered as teasers.
| No |
ArticleBean
|
Headline
| The article's headline. | No |
Text
| The article's text. | Yes | |
Abstract
|
The article's abstract which is extracted from property
Text
automatically.
| Yes* | |
Image
| An optional link to an image. | No | |
ImageBean
|
MimeType
| The image data's mime type. | Yes |
Data
| The binary data. | No |
Table 4.4. Bean Properties in the DataView Example. (*) The computation of property "abstract" is not expensive by itself but the access of property "text" only.
The JSP templates for rendering the beans are modeled as follows:
PageBean.jsp
<html> <head> <title><c:out value="${self.title}"/></title> </head> <body> <div class="content"><cm:include self="${self.content}"/> </div> <div class="navigation"> <ul> <c:forEach items="${self.navigation}" var="page"> <li><a href="<cm:link target='${page}'/>"> <c:out value="${page.title}"/></a></li> </c:forEach> </ul> </div> </body> </html>
ArticleBean.jsp
<h1><c:out value="${self.headline}"/></h1> <div><c:out value="${self.text}"/></div> <c:if test="${!empty self.image}"> <img src="<cm:link target='${self.image}'/>" alt="image"/> </c:if>
OverviewBean.jsp
<c:forEach items="${self.teasers}" var="article"> <h2><c:out value="${article.headline}"/></h2> <p> <c:out value="${article.abstract}"/> [<a href="<cm:link target='${article.page}'/>">more</a>] </p> </c:forEach>
Considering the above mentioned settings, the following
dataviews.xml
file
can be derived:
<?xml version="1.0"?> <dataviews xmlns= "http://www.coremedia.com/2004/objectserver/dataviewfactory"> <dataview appliesTo="com.mycompany.PageBean"> <property name="content" associationType="dynamic"/> <property name="navigation" associationType="static"/> </dataview> <dataview appliesTo="com.mycompany.ArticleBean"> <property name="page" associationType="static"/> <property name="text"/> </dataview> <dataview appliesTo="com.mycompany.OverviewBean"> <property name="teasers" associationType="dynamic"/> </dataview> <dataview appliesTo="com.mycompany.ImageBean"> <property name="mimeType"/> </dataview> </dataviews>
All expensive associations (PageBean#Navigation
and
ArticleBean#Page
) are declared to be data viewed using the default
association type "static". Please note, that
OverviewBean#Page
is not marked
here since this is not accessed by the templates.
PageBean#Content
and
OverviewBean#Teasers
are marked with the association type "dynamic" although
they are not expensive: Instead they are used making
ArticleBean
recursively
reachable from the PageBean
. Finally, the non-associating but expensive
properties
ArticleBean#Text
and
ImageBean#MimeType
are marked
for caching as well.
ArticleBean#Abstract
is not marked here because it
benefits from the already cached ArticleBean#Text
.
Keep in mind that a perfect data view configuration depends on a lot of circumstances.
Let's say that the underlying contents are updated very rarely on the one hand but
accessed very often on the other hand. In order to reduce the number of cache read
operations, some property associations might be switched to "composition". An
additional "teaser" data view might be introduced in order to cache the ArticleBean
's
different views (overview and detail) with separate objects.
<dataviews xmlns= "http://www.coremedia.com/2004/objectserver/dataviewfactory"> <dataview appliesTo= "com.coremedia.objectserver.dataviews.examples.PageBean"> <property name="content" associationType="composition"/> <property name="navigation" associationType="static"/> </dataview> <dataview appliesTo= "com.coremedia.objectserver.dataviews.examples.ArticleBean"> <property name="text"/> </dataview> <dataview appliesTo= "com.coremedia.objectserver.dataviews.examples.ArticleBean" name="teaser"> <property name="abstract"/> <property name="page" associationType="static"/> </dataview> <dataview appliesTo= "com.coremedia.objectserver.dataviews.examples.OverviewBean"> <property name="teasers" associationType="composition" dataview="teaser"/> </dataview> <dataview appliesTo= "com.coremedia.objectserver.dataviews.examples.ImageBean"> <property name="mimeType"/> </dataview> </dataviews>