Creating Content Beans

Overview

To expose custom document types in your site, you must wrap the corresponding JCR nodes in a HippoBean object. A content bean is a lightweight Java object (POJO) that represents a JCR node. During a request, you can access the content bean for the current page using:

HstRequestContext ctx = RequestContextProvider.get(); ctx.getContentBean()

For details on the request context, see the HstRequestContext documentation.

By default (as of version 13.2.0), Bloomreach Content dynamically generates content bean classes at runtime for document types created with the Document Type Editor. You can also generate these classes using the Beanwriter tool in the Essentials setup application. In some cases, you may need to customize the generated class or create one manually.

Mapping a Content Bean to a Document Type

To map a content bean to a JCR node type, annotate the class with org.hippoecm.hst.content.beans.Node and specify the jcrType parameter:

@Node(jcrType="myproject:textdocument") public class TextDocument extends HippoDocument { ... }

Refer to Automatic Scanning for instructions on enabling classpath scanning for content bean classes.

Mapping Primitive Fields

To expose JCR node properties in components and templates, implement getter methods in your bean. The return type of each getter must match the property's type.

Example getter for a string property:

public String getTitle() { return getSingleProperty("myproject:title"); }

The following table lists primitive CMS fields, their JCR property types, and the corresponding Java types:

Primitive CMS fieldJCR property typeJava typesRemarks
BooleanBooleanboolean
String, Text, LabelStringjava.lang.StringThe Text field provides a multi-line input in the CMS.
Date, CalendarDateDatejava.util.Calendar
Decimal NumberDoubledouble
Integer NumberLonglong
DocbaseStringjava.lang.StringStores the JCR UUID of the selected document as a string.
Formatted textStringjava.lang.StringUse the formattedText attribute of the <hst:html /> tag in your template.
PasswordStringjava.lang.StringThis field only hides input in the CMS UI. The value is not encrypted in storage.

Mapping Compound Fields

Compound fields in document types are stored as child nodes in JCR. The getter method in your bean should return the appropriate bean type that represents the child node.

Examples of getters for compound fields:

public HippoBean getRelatedDocument() { return getLinkedBean("myproject:relateddocument", HippoBean.class); }
public List<HippoGalleryImageSet> getImages() { return getLinkedBeans("myproject:images", HippoGalleryImageSet.class); }
public HippoHtml getDescription() { return getHippoHtml("myproject:description"); }

For custom compound types, implement your own bean classes. For standard compounds provided by Bloomreach, use the following mappings:

CMS Compound fieldJCR node typeMatching Bean typeRemarks
Rich Text Editorhippostd:htmlorg.hippoecm.hst.content.beans.standard.HippoHtmlUse the <hst:html /> tag in your template.
imagelinkhippogallerypicker:imagelinkorg.hippoecm.hst.content.beans.standard.HippoGalleryImageSetBeanUse HippoItem#getLinkedBean to retrieve the target bean. Use #getBean to access the child node containing the link.
Resourcehippo:resourceorg.hippoecm.hst.content.beans.standard.HippoResourceBean
Linkhippo:mirrororg.hippoecm.hst.content.beans.standard.HippoMirror

Preventing Beanwriter from Modifying Custom Methods

You can use the Beanwriter to generate new bean classes or methods for new fields, even if you have custom content bean classes. To prevent the Beanwriter from modifying existing methods in your beans:

  • Annotate the method with @HippoEssentialsGenerated.
  • Set the internalName parameter to the JCR property name.
  • Set the allowModifications parameter to false.

Example:

@HippoEssentialsGenerated(internalName = "myproject:title", allowModifications = false) public String getTitle() { return getSingleProperty("myproject:title"); }

To prevent modifications to the entire bean class, update the class-level annotation with the same parameters.

Persistence Support

By default, content beans are read-only and only provide getter methods. If you need to persist content submitted by site visitors, implement setter methods and the [org.hippoecm.hst.content.beans.ContentNodeBinder](/build/component-development/hstcomponent-persistable-annotation-and-workflow) interface in your content bean. This enables persistence for the bean.

Share Feedback
Page: /build/content-beans-translations/creating-content-beans
Section: Build
Category *
Creating Content Beans | Bloomreach Content Documentation