Friday 31 August 2018

Addons in Hybris

* Create new add-on and install created an addon to your storefront.
* Create new jsp in addon ,display it in storefront

Introduction to SAP Hybris AddOn ’s::-
---------------------------------------------------
1. Addons are built on top of the existing SAP Hybris Commerce to extend the functionality of the SAP    
    Hybris Commerce Accelerator.
2.  Addons are a form of extension that allows you to add front-end files e.g. JSP, CSS, and JS from within your
AddOn.
3.  This is instead of editing the core code of SAP Hybris which is considered against best practice.

What advantages does an AddOn have?
----------------------------------------------------------------
 – You adhere to best practices in SAP Hybris development.
– You can safely upgrade your core SAP Hybris code without the need for excessive development.
– You can add and remove the AddOn, at will, without refactoring or otherwise rewriting code.
How do I create an SAP Hybris AddOn?
---------------------------------------------------
1) Navigate to your hybris/bin/platform folder
2) Run the command
 setantenv.sh (Linux) or setantenv.bat (Windows)
3) Run the following command:

ant extgen -Dinput.template=yaddon -Dinput.name=myaddonname -Dinput.package=com.myappname




 If you run the command above, you would find a folder named myaddonname in your hybris/bin/custom folder.
4) You now need to modify your localextension.xml file to include the extension you just created. Add the following line:
<extension name=”myaddonname />
5) You will need to rebuild your Hybris system and perform an ant clean all.

Create 
‘Electronics’ add on and install created addon to ‘trainingstorefront’
=============================================================================

Addon Name ::ElectronicsK



Step 1:- Creating Addon

ant extgen -Dinput.template=yaddon -Dinput.name=ElectronicsK -Dinput.package=com.training

Step 2:- Installing Electronics Addon to "trainingstorefront".


Ant addoninstall -Daddonnames="Electronics" -DaddonStorefront.yacceleratorstorefront="trainingstorefront"



Create new jsp in an addon, display it in the storefront:-
-------------------------------------------------------------

1. Create a controller and calling existing facade to get data.
2. Add the required extensions in extensioninfo.xml
3. Add componentscan in ***-web_spring.xml file
4. Write a JSP to display data.


Step 1:- Create a controller and calling existing facade to get data.


Create in this package acceleratoraddon/web/src/com/controllers/pages



 package com.controllers.pages;
  
import de.hybris.platform.cms2.exceptions.CMSItemNotFoundException;
import de.hybris.platform.commercefacades.user.data.ProductData;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.facade.ProductFacade;

@Controller
@RequestMapping(value = "/addon")
public class ElectronicsAddonController
{
@Resource(name = "ProductFacade")
private ProductFacade ProductFacade;

private static final Logger LOG = Logger.getLogger(ElectronicsAddonController.class);

@RequestMapping(value = "/products", method = RequestMethod.GET)
public String getProductDetails(final Model model) throws CMSItemNotFoundException
{
LOG.info("########## ProductDetailsController updateOldPassword() method #####");

final List<ProductData> ProductData = ProductFacade.getProductDetails();

model.addAttribute("ProductData", ProductData);

return "addon:/Electronics/pages/electronicsproductdetails";
}
}

Step 2:-  Add the required extensions in extensioninfo.xml


<extensioninfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="extensioninfo.xsd">

<extension abstractclassprefix="Generated" classprefix="Electronics" description="Electronicsextension" managername="ElectronicsManager" 
managersuperclass="de.hybris.platform.jalo.extension.Extension"
 name="Electronics">
     
<requires-extension name="addonsupport"/>
<requires-extension name="acceleratorstorefrontcommons"/> 
       <requires-extension name="trainingfacades"/>
<coremodule generated="true" manager="com.jalo.ElectronicsManager" packageroot="com.training"/>

<meta key="classpathgen" value="false"/>

</extension>

</extensioninfo>

Step 3:-  Add componentscan in ***-web_spring.xml file

 <context:component-scan base-package="com.controllers.pages" />

Step 4:-  Write a JSP to display data

* Create JSP in this folder   acceleratoraddon/web/webroot/WEB-INF/views   in the package  
responsive.pages


electronicsproductdetails.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.lang.*" %>
<%@ taglib prefix="template" tagdir="/WEB-INF/tags/responsive/template" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 

<h2>Electronics JSP Page</h2>
<c:if test="${not empty ProductData}">
<table>
   <tr>
    <th><spring:theme code="electronics.product.code"/></th>
    <th><spring:theme code="electronics.product.name"/></th> 
   <th><spring:theme code="electronics.product.description"/></th>
           </tr>
<c:forEach items="${ProductData}" var="ProductData">
<tr>
<td>${ProductData.code}</td>
<td>${ProductData.name}</td>
<td>${ProductData.description}</td>
</tr>
</c:forEach>
</table>
</c:if>


7 comments:

  1. Excellent explanation ....Great job bro...Please keep adding new concepts regularly..
    Thank you...

    ReplyDelete
  2. Hello,

    Can you please inform what should be the url to hit the new api ?

    I followed the same steps and tried to hit "https://localhost:9002/youtubeaddon/addon/products" but it opens hac instead. As of now, my api is just printing a message on console. Not getting any error as well.

    ReplyDelete
    Replies
    1. I was able to resolve this. The api had to be "https://localhost:9002/trainingstorefront/addon/products/?site=electronics". Thanks for the tutorial.

      Delete
  3. Hello,

    I followed the same steps and tried to hit "https://localhost:9002/addon/products" but it showed me "HTTP Status 404 – Not Found" Please help me to resolve the issue

    ReplyDelete
    Replies
    1. Your Addon is installed on trainingstorefront extension so ur web root path in the url should be of trainingstorefront as before only further relative path is controller specific from addon so it becomes as below: https://localhost:9002/trainingstorefront/addon/products/?site=electronics

      Delete
  4. i wrote a simple controller and call getContentPageForLabelOrId with parameter null and return a jsp page ; it is giving 404 for my reuest path

    ReplyDelete