Showing posts with label WCMUse class sample. Show all posts
Showing posts with label WCMUse class sample. Show all posts

Tuesday, 13 February 2018

WCMUsePojo class & Alternatives in AEM?

AEM’s component development needs a back end logic to retrieve values from back end. Sightly is a templating language which together with WCMPojo helps to create components.

This approach(Sightly + WCMUseClass) provided better decoupling of the presentation layer vs business logic, thus code will be more maintainable and also easier to debug. AEM 6.1 or 6.2 uses WCMUsePojo class (or even Sling Models) for back end logic. Adobe recommends Sling Models as a best way of implementing AEM WCM Components with version AEM 6.3.

So in Brief,

We can use simple Pojo (without extending Adobe's class) with java-use-api. Cases where resources/services are not easily available, we can extend WCMUsePojo to get the ability to use resources/services. In an advanced way, we can go with Sling Models which will give more flexibility and ease, because it uses annotations.

Important Sling Annotation Reference are given below.

@Model : declares a model class or interface

@Inject : marks a field or method as injectable

@Named : declare a name for the injection (otherwise, defaults based on field or method name).

@Optional : marks a field or method injection as optional

@Source : explicitly tie an injected field or method to a particular injector (by name). Can also be on other annotations.

@Filter : an OSGi service filter

@PostConstruct : methods to call upon model option creation (only for model classes)

@Via : change the adaptable as the source of the injection

@Default : set default values for a field or method

@Path : only used together with the resource-path injector to specify the path of a resource

@Exporters/@Exporter/@ExporterOptions/@ExporterOption : for Exporter Framework

WCMUsePojo Vs Sling Models


  • Mixed POJOs - Pure POJOs
  • Extends from WCMUsePojo - Standalone class with '@Model' annotation and having no keyword
  • More code required to retrieve common objects or property values - Easier methods to retrieve common objects or property values
  • Uses  Felix annotation '@Reference' to reference to an available OSGI service - Uses '@Inject' or '@OSGiService'
  • In case of WCMUsePojo, we have to overwrite the activate() method - init() method will be called in the @PostConstruct annotation
  • Not annotation driven - Annotation driven

Thursday, 23 February 2017

Sample WCMUse Java File for AEM

The VehicleService.Java WCMUse class below, build and deploy it using maven to your AEM.

    package com.aem;

    import org.apache.sling.api.SlingHttpServletRequest;
    import com.day.cq.wcm.api.Page;
    import java.util.HashMap;
    import java.util.Map;
    import com.adobe.cq.sightly.WCMUse;

    public class VehicleService extends WCMUse {
        private Map<String, String> map;

        @Override
        public void activate() throws Exception {
            SlingHttpServletRequest request = getRequest();
            Page currentPage = getCurrentPage();
            createVehicleDetails(request, currentPage);
        }


        /**
         * This will Create vehicle map
         *
         * @return
         */
    private void createVehicleDetails(SlingHttpServletRequest request, Page currentPage) {
        map = new HashMap<String, String>();
        map.put("name", "NewVehicle");
        map.put("price", "1000");    
        }

        /**
         * This will return vehicle
         *
         * @return vehicle map
         */
   
        public Map<String,String> getVehicleDetails() {
       
            return map;
        }
   
    }

--------------Similar Posts:----------
-------------------------------------------