HstComponent Java Class Examples

This page provides example implementations of custom HstComponent Java classes for Bloomreach Content. These examples demonstrate how to access content beans, handle request parameters, retrieve component configuration, and implement a basic search component.

Accessing Content Beans in an HstComponent

The following example shows a simple HstComponent that retrieves the HippoBean associated with the resolved sitemap item and sets it as a request attribute. It also sets the root content bean for the current (sub)site on the request.

public class Detail extends BaseHstComponent { @Override public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException { HstRequestContext ctx = request.getRequestContext(); // Retrieve the content bean for the current sitemap item and set it as a request attribute. HippoBean documentBean = ctx.getContentBean(); request.setAttribute("document", documentBean); // Retrieve the content bean as a specific type (returns null if not of type NewsDocument). NewsDocument newsDocument = ctx.getContentBean(NewsDocument.class); // Retrieve the root content bean for the current (sub)site and set it as a request attribute. HippoBean rootBean = ctx.getSiteContentBaseBean(); request.setAttribute("root", rootBean); // Retrieve the base bean for assets, for example to use in an HstQuery. HippoFolderBean assetBaseBean = getAssetBaseBean(request); // Retrieve the base bean for gallery items, for example to use in an HstQuery. HippoFolderBean galleryBaseBean = getGalleryBaseBean(request); } }

Accessing Namespaced and Public Request Parameters

This example demonstrates how to access both namespaced and public request parameters within an HstComponent.

public class Detail extends BaseHstComponent { // Retrieve the namespaced request parameter 'page'. // For example, if the component's namespace is r1_r2, // only the request parameter 'r2_r2:page' will be accessible here. String strPage = request.getParameter("page"); // Retrieve the public request parameter 'query' without namespacing. // This is useful when the parameter is submitted via an HTML form // and needs to be accessed by a different HstComponent. String query = getPublicRequestParameter(request, "query"); }

Retrieving HstComponent Configuration Parameters Without ParametersInfo

You can access component configuration parameters directly, although using the ParametersInfo annotation is preferred. The following example shows several ways to retrieve component parameters.

public class Detail extends BaseHstComponent { // Retrieve the 'pageSize' parameter with property placeholders resolved. String pageSize = getComponentParameter("pageSize"); // Retrieve the raw 'pageSize' parameter without resolving property placeholders. String pageSizeRaw = getComponentConfiguration().getRawParameters().get("pageSize"); // Retrieve the 'pageSize' parameter without inheriting from ancestor components. String localPageSize = getComponentLocalParameter("pageSize"); // Retrieve the raw local 'pageSize' parameter without inheritance and without resolving property placeholders. String localRawPageSize = getComponentConfiguration() .getRawLocalParameters().get("pageSize"); }

Example: Basic Search HstComponent

The following example implements a basic search component. It does not use the ParametersInfo annotation (which is recommended for production code). The component sets the search result, current page, query, and a list of available page numbers as request attributes. The renderer (JSP or Freemarker) can iterate over the result, which contains HippoBean instances for each hit.

public class Search extends BaseHstComponent { @Override public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException { // Retrieve the root content bean for the current (sub)site. HstRequestContext ctx = request.getRequestContext(); HippoBean scope = ctx.getSiteContentBaseBean(); if (scope == null) { throw new HstComponentException("Scope is not allowed to be null for a search"); } int pageSize = 10; String query = getPublicRequestParameter(request, "query"); if (query == null) { // Fallback to namespaced query parameter if public parameter is not present. query = request.getParameter("query"); } String crPageStr = request.getParameter("page"); int crPage = 1; if (crPageStr != null) { try { crPage = Integer.parseInt(crPageStr); } catch (NumberFormatException e) { throw new HstComponentException("Invalid page number '" + crPage + "'"); } } // Search for news items Class filterClass = getObjectConverter().getAnnotatedClassFor("myproject:news"); try { HstQuery hstQuery = ctx.getQueryManager().createQuery(scope, filterClass, true); hstQuery.setLimit(pageSize); if (query != null && !"".equals(query)) { Filter f = hstQuery.createFilter(); f.addContains(".", query); hstQuery.setFilter(f); } HstQueryResult result = hstQuery.execute(); request.setAttribute("result", result); request.setAttribute("crPage", crPage); request.setAttribute("query", query); request.setAttribute("totalSize", result.getTotalSize()); // Add pages if there are more results than the page size. if (result.getTotalSize() > pageSize) { List<Integer> pages = new ArrayList<Integer>(); int numberOfPages = result.getTotalSize() / pageSize; if (result.getTotalSize() % pageSize != 0) { numberOfPages++; } for (int i = 0; i < numberOfPages; i++) { pages.add(i + 1); } request.setAttribute("pages", pages); } } catch (QueryException e) { throw new HstComponentException("Exception occured during creation or execution of HstQuery.", e); } } }
Share Feedback
Page: /build/component-development/hstcomponent-java-class-examples
Section: Build
Category *