HYBRIS - How components and slots work in JSP file

2020-07-18 10:58发布

问题:

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.

回答1:

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.



回答2:

I haven't created a template or a page or slot. I've used the one that Hybris brings on his example store. I only have created a new component CustomCMSImageComponent like a copy from CMSImageComponent.

  1. Define the new component on file: mystorecore-items.xml

       <deployment table="CustomCMSImageComponent" typecode="20003"/>
    </itemtype>
    

  2. Create a new controller for this component CustomCMSImageComponentController.java and a view customcmsimagecomponent.jsp

  3. Indicate that CustomCMSImageComponentController.java should be the controller to this component in ControllerConstants.java

    String CustomCMSImageComponent = _Prefix + CustomCMSImageComponentModel._TYPECODE + _Suffix; // NOSONAR

  4. Create the instances of this component in AddToCartSlot (in the corresponding impex)

  5. Build proyect, start server, initialize (HAC) and synchronize (HMC)

Yesterday I found why is showing all components when I use this tag: in fact that's the component's name in slot and the slot's name in the page. So this question is resolved.



回答3:

Try following things:

  1. Sync catalog. You might have everything right but may have forgotten to sync the catalog after you added the components. This might be trivial but worth a check.
  2. Since you created your own component, I assume you created a JSP for your component as well (if it's extending SimpleCMSComponent)

    Use the name of the component jsp file excluding the word component in the var attribute of CMS Page Slot tag.

    If I had my jsp component contents in a file called mycartcomponent.jsp, then I would use it as

    For example,

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