How to generate a dynamic URL using in Oracl

2019-03-14 03:56发布

问题:

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>

回答1:

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>


回答2:

@Bated answer is pretty thorough but want to add some additional information about why it is so important to use the ATG dsp tags to achieve this.


ATG maintains your session in two ways:

  1. Via the jsessionid stored in a cookie
  2. Via the jsessionid stored in the query string

By using the <dsp> tags you ensure that you maintain the users session as it is quite possible that the user has cookies disabled and the dsp tags will revert to using the query string.

Using the html anchor tag is unsafe for this reason as the users session will only be maintained if they have cookies enabled.



回答3:

Please try the following snippet. You need to use dsp:getvalueof as shown below.

<dsp:droplet name="/atg/commerce/catalog/ProductLookup">
    <dsp:param param="element.id" name="id"/>
    <dsp:oparam name="output"><br/>
       <dsp:getvalueof id="id" param="element.id" idtype="java.lang.String">
           <dsp:a href="display_product.jsp?itemId=<%=id%>">
               Product Name: <b><dsp:valueof param="element.displayName"/></b><br/>
               Product description: <dsp:valueof param="element.description"/>
           </dsp:a>
       </dsp:getvalueof>
    </dsp:oparam>
</dsp:droplet>