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 field | JCR property type | Java types | Remarks |
|---|---|---|---|
| Boolean | Boolean | boolean | |
| String, Text, Label | String | java.lang.String | The Text field provides a multi-line input in the CMS. |
| Date, CalendarDate | Date | java.util.Calendar | |
| Decimal Number | Double | double | |
| Integer Number | Long | long | |
| Docbase | String | java.lang.String | Stores the JCR UUID of the selected document as a string. |
| Formatted text | String | java.lang.String | Use the formattedText attribute of the <hst:html /> tag in your template. |
| Password | String | java.lang.String | This 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 field | JCR node type | Matching Bean type | Remarks |
|---|---|---|---|
| Rich Text Editor | hippostd:html | org.hippoecm.hst.content.beans.standard.HippoHtml | Use the <hst:html /> tag in your template. |
| imagelink | hippogallerypicker:imagelink | org.hippoecm.hst.content.beans.standard.HippoGalleryImageSetBean | Use HippoItem#getLinkedBean to retrieve the target bean. Use #getBean to access the child node containing the link. |
| Resource | hippo:resource | org.hippoecm.hst.content.beans.standard.HippoResourceBean | |
| Link | hippo:mirror | org.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
internalNameparameter to the JCR property name. - Set the
allowModificationsparameter tofalse.
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.