HYBRIS - How components and slots work in JSP file?

Aída Morillas Muñoz picture Aída Morillas Muñoz · Jun 21, 2016 · Viewed 14.4k times · Source

Recently I am working with Hybris, and I can not understand how the components work.

I know how to create and define one, how to add them to the page I want, etc. But I do not understand how to use the tag <cms: component> in the jsp file.

In the slot AddToCartSlot from the product detail page, I added more components. I tried to call my component as does the standard and comment his lines.

By default, it is called the component as follows:

<cms:pageSlot position="AddToCart" var="component">
   <cms:component component="${component}" />
</cms:pageSlot>

So I tried to call my component as well, but does not work:

<cms:pageSlot position="MyComponent" var="component">
   <cms:component component="${component}" />
</cms:pageSlot>

So my lines commented and uncommented his lines, and all components are shown on the page. But for me this makes no sense because in the position attribute of the tag cms:pageSlot should received the id of a slot and not the id of a component to show all components slot. However, putting the id AddToCartinstead of AddToCartSlot is the only way that all components are displayed on the page.

Now you will think 'what the problem if the components are being displayed on the web?', well, the problem is that these components are not going through the java controller that corresponds to them (despite being created and declared in the com.myStore.storefront.controllers.ControllerConstants.java file). In addition, I would like to understand why it is not working properly.

I followed the steps of Wki Hybris and I found that everything is declared as it is to another custom component that's working properly. I can not find differences and I can not understand why not pass my controller or why the tag does not work as it should with the id of the slot, but it "works" when I use the identifier of a component.

Really, any ideas will help.

Thank you very much.

Answer

dj_frunza picture dj_frunza · Jun 24, 2016

This is how the controller should look like in order for Hybris to use it:

@Controller("CustomCMSImageComponentController")
@RequestMapping(value = ControllerConstants.CustomCMSImageComponent )// now the controller is mapped to "/view/CustomCMSImageComponentController"
public class CustomCMSImageComponentController extends AbstractCMSComponentController<CustomCMSImageComponentModel> {
    @Override
    protected void fillModel(final HttpServletRequest request, final Model model,
                       final CustomCMSImageComponentModelcomponent) {
    //here the spring Model(model method parameter) should be filled with what is needed to dynamically render in JSP
    }
}

The @Controller annotation is used by Spring to instantiate the CustomCMSImageComponentController and keep that instance(bean) in the spring application context.

When rendering the CustomCMSImageComponent Hybris searches after the bean with the name "CustomCMSImageComponentController" in the spring application context in order to find the Controller associated with the component and if it does not find anything the DefaultCMSComponentController will be used.