# Controller for every view?

**URL:** <https://community.bloomreach.com/t/controller-for-every-view/744>\
**Category:** Experience Manager (PaaS/OnPrem)\
**Created:** [January 21, 2019, 1:38pm UTC](https://community.bloomreach.com/t/controller-for-every-view/744 "2019-01-21T13:38:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marsdev](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@marsdev](https://community.bloomreach.com/u/marsdev)\
**Post date:** [January 21, 2019, 1:38pm UTC](https://community.bloomreach.com/t/controller-for-every-view/744/1 "2019-01-21T13:38:34Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![woonsanko](https://avatars.discourse-cdn.com/v4/letter/w/b5a626/32.png) [@woonsanko](https://community.bloomreach.com/u/woonsanko)\
**Post date:** [January 21, 2019, 2:13pm UTC](https://community.bloomreach.com/t/controller-for-every-view/744/2 "2019-01-21T14:13:28Z")

</div>

> [@marsdev](#):
>
> I think I must be missing this in the documentation somewhere.
> 
> I’ve read through [Hello World - Bloomreach Experience Manager (PaaS/Self-Hosted) - The Fast and Flexible Headless CMS](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?

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:

```auto
/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:

```auto
<@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

---

<div class="post-metadata">

**Author:** ![marsdev](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@marsdev](https://community.bloomreach.com/u/marsdev)\
**Post date:** [January 21, 2019, 2:29pm UTC](https://community.bloomreach.com/t/controller-for-every-view/744/3 "2019-01-21T14:29:52Z")

</div>

That worked perfect. Thank you very much.

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