java HYBRIS - JSP 文件中的组件和插槽如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37942061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:57:54  来源:igfitidea点击:

HYBRIS - How components and slots work in JSP file?

javajspjsp-tagshybris

提问by Aída Morillas Mu?oz

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

最近我在使用 Hybris,我无法理解这些组件是如何工作的。

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 jspfile.

我知道如何创建和定义一个,如何将它们添加到页面我想,等,但我不明白如何使用标签<cms: component>jsp的文件。

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

AddToCartSlot产品详细信息页面的插槽中,我添加了更多组件。我试着像标准一样调用我的组件并评论他的台词。

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:pageSlotshould received the id of a slot and not the id of a component to show all components slot. However, putting the id AddToCartinstead of AddToCartSlotis the only way that all components are displayed on the page.

所以我的行注释和取消注释他的行,所有组件都显示在页面上。但对我来说这没有意义,因为在标签的位置属性中cms:pageSlot应该接收插槽的 id 而不是组件的 id 来显示所有组件插槽。但是,使用 idAddToCart代替 ofAddToCartSlot是所有组件都显示在页面上的唯一方式。

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.javafile). In addition, I would like to understand why it is not working properly.

现在你会想“如果组件显示在 web 上有什么问题?”,好吧,问题是这些组件没有通过对应于它们的 java 控制器(尽管在com.myStore.storefront.controllers.ControllerConstants.java文件中创建和声明)。另外,我想了解为什么它不能正常工作。

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.

我遵循了 Wki Hybris 的步骤,我发现所有内容都被声明为另一个正常工作的自定义组件。我找不到差异,我不明白为什么不通过我的控制器,或者为什么标签不能像插槽的 id 那样工作,但是当我使用组件的标识符时它“有效”。

Really, any ideas will help.

真的,任何想法都会有所帮助。

Thank you very much.

非常感谢你。

采纳答案by dj_frunza

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

这是控制器的外观,以便 Hybris 使用它:

@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.

Spring 使用 @Controller 注释来实例化 CustomCMSImageComponentController 并将该实例(bean)保存在 spring 应用程序上下文中。

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.

渲染 CustomCMSImageComponent 时,Hybris 在 spring 应用程序上下文中搜索名为“CustomCMSImageComponentController”的 bean 以找到与组件关联的控制器,如果找不到任何内容,则将使用 DefaultCMSComponentController。

回答by Aída Morillas Mu?oz

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 CustomCMSImageComponentlike a copy from CMSImageComponent.

我还没有创建模板、页面或槽。我使用了 Hybris 在他的示例商店中带来的那个。我只创建了一个新组件,CustomCMSImageComponent例如从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.javaand a view customcmsimagecomponent.jsp

  3. Indicate that CustomCMSImageComponentController.javashould 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)

  1. 在文件中定义新组件: mystorecore-items.xml

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

  2. 为这个组件创建一个新的控制器CustomCMSImageComponentController.java和一个视图customcmsimagecomponent.jsp

  3. 表示CustomCMSImageComponentController.java应该是这个组件的控制器ControllerConstants.java

    String CustomCMSImageComponent = _Prefix + CustomCMSImageComponentModel._TYPECODE + _Suffix; // 声纳

  4. AddToCartSlot(在相应的impex中)创建此组件的实例

  5. 构建项目、启动服务器、初始化 (HAC) 和同步 (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.

昨天我发现为什么当我使用这个标签时会显示所有组件:事实上,这是插槽中组件的名称和页面中插槽的名称。所以这个问题解决了。

回答by Abubakkar

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 componentin the varattribute 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>
    
  1. 同步目录。您可能一切正常,但在添加组件后可能忘记同步目录。这可能是微不足道的,但值得一试。
  2. 由于您创建了自己的组件,因此我假设您也为您的组件创建了一个 JSP(如果它扩展了 SimpleCMSComponent)

    使用不含字部件JSP文件的名称componentvarCMS页槽标签的属性。

    如果我在一个名为 的文件中包含我的 jsp 组件内容mycartcomponent.jsp,那么我会将它用作

    例如,

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