This article covers a Hippo CMS version 7.7. There's an updated version available that covers our most recent release.

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 {

    public static final Logger log = LoggerFactory.getLogger(Detail.class);

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

        // 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 = getContentBean(request);
        request.setAttribute("document",documentBean);

        // or, getting the content bean strong typed and null if the bean is not of type NewsDocument
        NewsDocument newsDocument = getContentBean(request,  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 = getSiteContentBaseBean(request);
        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)

Accessing component configuration parameters:

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 = getParameter("pageSize", request);

 // 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 = getLocalParameter("pageSize", request);

 // 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 TODO-LINK 5._HstComponent_ParametersInfo_annotation.html5. HstComponent ParametersInfo annotation . On the HstRequest, the search result, the crPage, the query and an list of available page numbers is set. The renderer (jsp / freemarker) can iterate through the result which returns HippoBean's for every hit.

Search not using ParametersInfo annotation:

public class SearchNoParameterInfo extends BaseHstComponent {

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

       // get the root content bean for the current (sub)site. This is the scope to search below
       HippoBean scope = getSiteContentBaseBean(request);

       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");
       }

       if(scope == null) {
           throw new HstComponentException("Scope is not allowed to be null for a search");
       }

       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("myhippoproject:news");

        try {
            HstQuery hstQuery = 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);
        }
    }
}