Solutons Lounge

How to create URLS to read information from a cont… – Adobe Experience League Community


Hello, currently I have a servlet that sends to call an api that brings a JSON, this JSON are products and can be X amount, in order once called these products I save them in content fragments.

 

And the next part is that I have a dynamic component, the component doesn’t show information until by means of the url you indicate him that product you want to visualize to achieve this you have the following link

 

domain.com/products

 

you will have to indicate in the url that product you want to visualize with a selector of the following way:

 

domain.com/products.product1

 

the variable product1 is a variable named product_title and is the content fragment product id, and this way the component will go to look for in the content fragment of the jcr that product and will show the information. It works everything and perfect, but now what I want to do is that instead of using

 

domain.com/products.product1

 

use now

 

domain.com/products/product1

 

That is to say to use a / instead of .

 

The problem is that if I use a / AEM detects me as if looking for a page created and existing in the path after products, which is not so, what can I do to add that rule or modification?

This is my component model code:

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})

public class ProductModel {

    private static final Logger log = LoggerFactory.getLogger(ProductModel.class);

    @Self

    private SlingHttpServletRequest request;

    @SlingObject

    private ResourceResolver resourceResolver;

    @Optional

    private String productTitle;

    // Declaration of the rest of the values

    @PostConstruct

    protected void init() {

        String[] selectors = request.getRequestPathInfo().getSelectors();

        String cfProductName = selectors[0];

        Resource productResource = resourceResolver.getResource(“/content/dam/tfs/us/en/test-folder/products/” + cfProductName);

        if (productResource != null) {

            ContentFragment contentFragment = productResource.adaptTo(ContentFragment.class);

            if (contentFragment != null) {

                Iterator<ContentElement> elements = contentFragment.getElements();

                while (elements.hasNext()) {

                    ContentElement element = elements.next();

                    switch (element.getName()) {

                        case “product_title”:

                            productTitle = getValueOrDefault(element);

                            break;

                        case “brand_name”:

                            brandName = getValueOrDefault(element);

                            break;
                         // Rest of the cases of the values

                    }

                }

            } else {

                log.warn(“Resource is not a Content Fragment: {}”, productResource.getPath());

            }

        } else {

            log.warn(“No product found for productTitle: {}”, cfProductName);

        }

    }

    private String getValueOrDefault(ContentElement element) {

        return element != null && element.getContent() != null ? element.getContent() : “”;

    }

    private String[] getArrayOrDefault(ContentElement element) {

        return element != null && element.getContent() != null ? element.getContent().split(“,”) : new String[]{};

    }

    public String getProductTitle() {

        return productTitle;

    }

   // Rest of the getters methods

}



Source link

Exit mobile version