HstComponent Java Class Examples

Below is the code for a very simple HstComponent that sets the  HippoBean that belongs to the resolved sitemap item on the HstRequest ( HttpServletRequest). We also set the site content base bean, which is the 'root' bean for the current (sub)site on the request as an example.

Simple HstComponent extending BaseHstComponent:

public class Detail extends BaseHstComponent {
  @Override
  public void doBeforeRender(final HstRequest request,
                             final HstResponse response) throws HstComponentException {

      HstRequestContext ctx = request.getRequestContext();
      // get the content bean for the current resolved sitemap item and
      // set it on the request to make it available for
      // the renderer like jsp or freemarker
      HippoBean documentBean = ctx.getContentBean();
      request.setAttribute("document",documentBean);

      // or, getting the content bean strong typed and null if the bean is
      // not of type NewsDocument
      NewsDocument newsDocument = ctx.getContentBean(NewsDocument.class);

      // get the content bean for the root of the current (sub)site and set
      // it on the request to make it available for
      // the renderer like jsp or freemarker
      HippoBean  rootBean = ctx.getSiteContentBaseBean();
      request.setAttribute("root",rootBean);

      // get the base bean where all assets are stored, for example to use
      // in a HstQuery
      HippoFolderBean assetBaseBean = getAssetBaseBean(request);

      // get the base bean where all gallery items are stored, for example
      // to use in a HstQuery
      HippoFolderBean galleryBaseBean = getGalleryBaseBean(request);
    }

}

Example about accessing namespaced and public request parameters:

Accessing namespaced and public request paramaters:

public class Detail extends BaseHstComponent {
    // tries to get the namespaced request parameter 'page'.
    // For example, when the namespace of this component is
    // r1_r2, then, only when the request parameter is
    // 'r2_r2:page', it's value will be assigned to strPage
    // below

    String strPage = request.getParameter("page");

    // getPublicRequestParameter fetches the request parameter 'query' without
    // namespacing! This is useful when for example the query is submitted
    // through a html form, and needs to be rendered in a different
    // HstComponent than the one that dispatched to the jsp that created the
    // search form.
    String query = getPublicRequestParameter(request, "query");
}

Example about getting hst component configuration parameters  without  ParametersInfo annotation (note that using ParametersInfo is  preferred, see HstComponent ParametersInfo annotation)

public class Detail extends BaseHstComponent {
    // get the page size parameter from the HstComponent. If the parameter
    // value contained property place holders like ${1} then these are
    // resolved
    String pageSize = getComponentParameter("pageSize");

   // get the page size parameter from the HstComponent. If the parameter
   // value contained property place holders like ${1} these are *not*
   // resolved
   String pageSizeRaw = getComponentConfiguration().getRawParameters()
                                                             .get("pageSize");

   // get the page size parameter from the HstComponent *without inheritance*
   // from ancestor components
   String localPageSize = getComponentLocalParameter("pageSize");

   // get the page size parameter from the HstComponent *without inheritance*
   // from ancestor components and *without* having property place holders
   // like ${1} resolved
   String localRawPageSize = getComponentConfiguration()
                                 .getRawLocalParameters().get("pageSize");
}

The example below is an example of an HstComponent which is a Search implementation.  Note that it does not use ParametersInfo annotation, which is the preferred way, see Define a Component's Configuration Parameters. On the HstRequest, the search  result, the crPage, the query and a list of available page numbers is set. The renderer (jsp / freemarker) can iterate through the result which returns  HippoBean's for every hit.

public class Search extends BaseHstComponent {

    @Override
    public void doBeforeRender(final HstRequest request,
                               final HstResponse response) throws HstComponentException {

        // get the root content bean for the current (sub)site.
        // This is the scope to search below
        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; // the free text to search on
        String query = getPublicRequestParameter(request, "query");
        if (query == null) { // test namespaced query parameter
            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 (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);
        }
    }
}
Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?