Controller for every view?

I think I must be missing this in the documentation somewhere.

I’ve read through https://www.onehippo.org/trails/deep-dive/hello-world.html and the GoGreen trails.

I have created Document Types and Documents based on them. If I want to display that content in a view do I have to create a custom Component (controller) every time?

I have and it works, but I’m wondering if it could be done with configuration instead?

So for every “page” that uses the custom Document Type I need to have a component with a method like:
@Override
public void doBeforeRender(HstRequest request, HstResponse response) throws HstComponentException {
super.doBeforeRender(request, response);
final HstRequestContext ctx = request.getRequestContext();

        // Retrieve the document based on the URL
        HeroFrontSixCardDocument document = (HeroFrontSixCardDocument) ctx.getContentBean();

        if (document != null) {
            // Put the document on the request
            request.setAttribute("document", document);
        }
    }

I don’t mind, I’m just wondering if I can configure this instead.

thanks and sorry if I missed this in the documentation.

John

You can skip HstComponent implementation and also possibly its configuration if you don’t need to add any custom business logic in that controller other than simply retrieving the mapped content document bean.

For example, in that scenario, you can skip the step 4, and simplify the step 6:

  • You can skip configuring /hst:project/hst:configurations/myproject/hst:components/simplecomponent.
  • Simply configure the component in the page without defining or referencing component, but only with template reference:
/hst:myhippoprojet/hst:configurations/myproject/hst:pages/home:
  hst:template: homepage

Now, your template can retrieve the mapped document without controller’s help like the following:

<@hst.defineObjects />
<@assign document=hstRequestContext.contentBean />
<#-- use document bean as done in the example from here -->

<@hst.defineObjects /> sets hstRequestContext, hstRequest and hstResponse as request attributes, so you can use it in templates afterward. HstRequestContext#getContentBean(), which is same as hstRequestContext.contentBean in the FreeMarker template example, is what you’ve seen in the controller code example.

[quote=“marsdev, post:1, topic:744, full:true”]So for every “page” that uses the custom Document Type I need to have a component with a method like:
@Override
public void doBeforeRender(HstRequest request, HstResponse response) throws HstComponentException {
super.doBeforeRender(request, response);
final HstRequestContext ctx = request.getRequestContext();

        // Retrieve the document based on the URL
        HeroFrontSixCardDocument document = (HeroFrontSixCardDocument) ctx.getContentBean();

        if (document != null) {
            // Put the document on the request
            request.setAttribute("document", document);
        }
    }

[/quote]
If you have code only to retrieve the mapped bean like the above, you don’t need to implement a controller; your template can do that. It is more meaningful to implement a custom HstComponent code when you need to have more complex business logics, possibly with other backend integrations.

Regards,

Woonsan

That worked perfect. Thank you very much.

I’m using Freemarker, so I just changed “<@assign” to “<#assign” in the template file.