Search code examples
javajspatgoracle-commerceatg-droplet

How to generate a dynamic URL using <dsp:a> in Oracle Commerce(ATG)


I'm trying to display list of products in a jsp using ProductLookup droplet as shown below. I'm also trying to give a hyperlink to navigate to product details page of the individual product.

<dsp:droplet name="/atg/commerce/catalog/ProductLookup">
    <dsp:param param="element.id" name="id"/>
    <dsp:oparam name="output"><br/>
    <dsp:a href="display_product.jsp?itemId=${id}">  
    Product display Name: 
    <b><dsp:valueof param="element.displayName"/></b><br/>
    Product description Name:
    <dsp:valueof param="element.description"/>
    </dsp:a>
    </dsp:oparam>
</dsp:droplet>

However, I'm facing issue while passing the id of the product to href tag of dsp:a. The resultant HTML has hardcoded ${id} as display_product.jsp?itemId=${id}. I'm getting list of products, but the URL is where I'm facing issue. How do I pass the value in element.id into the href attribute of dsp:a?

I've also tried the following, but no success.

1.

<dsp:a href="display_product.jsp?itemId=<%=out.print(element.id) %>">

2.

<dsp:a href="display_product.jsp?itemId=<%=out.print(id) %>">

3.

<dsp:getvalueof var="id" id="id" >
            <dsp:a href="display_product.jsp?itemId=${id}">
            Product display Name: 
            <b><dsp:valueof param="element.displayName"/></b><br/>
            Product description Name:
            <dsp:valueof param="element.description"/>
            </dsp:a>
            </dsp:getvalueof>

Solution

  • To pass parameters to another page you simply use the <dsp:param> tag as per the code fragment below (nested within your ProductLookup droplet):

    This is the old-school ATG approach:

     <dsp:a href="display_product.jsp">
         Product Name: <b><dsp:valueof param="element.displayName"/></b><br/>
         Product description: <dsp:valueof param="element.description"/>
         <%-- this will pass the itemId parameter and value--%>
         <dsp:param name="itemId" param="element.id"/>
     </dsp:a>
    

    The preferred approach is to use jstl EL variables which makes your jsp's cleaner and easier to read, as well as allowing more flexibility when referring to the values:

    <%-- name the "element" and convert to a map  --%>
    <dsp:tomap var="product" param="element" recursive="false"/>
    <dsp:a href="display_product.jsp">
         Product Name: <b>${ product.displayName }</b><br/>
         Product description: ${ product.description }
         <dsp:param name="itemId" value="${ product.id }"/>
    </dsp:a>
    

    I have used recursive="false" in the above example because you are only referencing to direct properties of a product. If you wanted to refer to properties of properties then you could do something like this (the code below is not tested but should give you the general idea):

    <dsp:tomap var="product" param="element" recursive="true"/>
    <dsp:img page="${ product.image.url }">
    

    UPDATE:

    As I continue to be amazed how few people understand how to do even the most basic thing such as passing parameter, I have included other DSP tags which can be used in conjunction with the <dsp:param>

    You can pass parameters for included JSP fragments which use the <dsp:include>

    <dsp:tomap var="product" param="element" recursive="false"/>
    <dsp:include page="fragments/myfragment.jsp">
        <dsp:param name="itemId" value="${ product.id } />
    </dsp:include>
    

    This approach will work for these tags as well:

    • <dsp:iframe>
    • <dsp:img>
    • <dsp:link>