Wednesday, September 30, 2015

ATG Droplet 101

First read the following:
  1. What is a Droplet? - http://learnoracleatg.blogspot.com/2014/10/art109-what-are-droplets-or-dynamo.html
  2. How to pass parameters to a Droplet from JSP? http://learnoracleatg.blogspot.in/2014/10/art110-how-to-pass-parameters-to.html
Here is what I did:
Step 1: I created a HelloWorldDroplet in modules.base 
/modules.base/src/atg/repository/HelloWorldDroplet.java
package atg.repository;

import java.io.IOException;
import javax.servlet.ServletException;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;
import atg.servlet.DynamoServlet;

public class HelloWorldDroplet extends DynamoServlet {

 public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response) throws IOException, ServletException{
  
  String myInput = request.getParameter("input");
  response.setContentType("text/html");
  
  myInput = myInput.concat(" appended from droplet");
  request.setParameter("concatenatedString", myInput);

  request.serviceParameter("CUSTOMOUTPUT", request, response);
  
  response.getOutputStream().println("Hello World\n");
 }
}

Step 2: A droplet is a component. So we need to create a configuration file. Here is the HelloWorldComponent.properties

/modules.base/config/atg/hello/HelloWorldComponent.properties


$class=wildcats.droplets.HelloWorldDroplet
Step 3: How to invoke a droplet from JSP?

/modules.store/j2ee-apps/Web/Web.war/hello/helloworld.jsp

<%@ taglib uri="/dspTaglib" prefix="dsp"%>
<dsp:page>
    <dsp:importbean bean="/atg/hello/HelloWorldComponent" />
    <html>
        <body>
        Here is the text from droplet<br>
            <dsp:droplet name="HelloWorldComponent">
            <dsp:param name="input" value="passed from jsp" />
 
            <dsp:oparam name="CUSTOMOUTPUT">
            <dsp:valueof param="concatenatedString" />
            </dsp:oparam>
            </dsp:droplet>
        </body>
    </html>
</dsp:page>

Step 4: Look at the modules.store's web.xml and find the context-root. If you do not have any add the following: /modules.store/j2ee-apps/Web/Web.war/WEB-INF/web.xml


<context-param>
  <param-name>context-root</param-name>
  <param-value>sams</param-value>
</context-param>

Step 5: From the above web.xml, we know how to access the jsp file. Type http://localhost:8080/myapp/hello/helloworld.jsp in your browser and see the following output in your browser:



More about Droplets: Out Of the box droplets:
  • I tried out how to use "ForEach" droplet. Here is what I did:
    Step 1: Create an employee component i.e a java file and a properties file.
/modules.base/src/atg/repository/EmployeeBean.java

package atg.repository;
/**
 *
 *
 * @version  : 1.0
 */
import java.util.ArrayList;
import java.util.List;
public class EmployeeBean {
        List<String> carsList = new ArrayList<String>();
        public List<String> getCarsList() {
            return carsList;
        }
        public void setCarsList(List<String> carsList) {
            this.carsList = carsList;
        }
}
/modules.base/config/atg/hello/EmployeeComponent.properties
$class=atg.repository.EmployeeBean
carsList=\
        Honda,\
        Skoda,\
        Mercedes,\
        VW

Step 2: Write the JSP which invokes the droplet:

/modules.store/j2ee-apps/Web/Web.war/hello/helloemployee.jsp


<%@ taglib uri="/dspTaglib" prefix="dsp"%>
<dsp:page>
    <dsp:droplet name="/atg/dynamo/droplet/ForEach">
    <dsp:param name ="array" bean="/atg/hello/EmployeeComponent.carsList" />
    <dsp:oparam name="outputStart">
        <p> The employee has following cars: <br/></p>
    </dsp:oparam>
     
    <ul>
        <dsp:oparam name = "output">
        <li><dsp:valueof param="element"></dsp:valueof></li>
        </dsp:oparam>
    </ul>
     
    <dsp:oparam name ="outputEnd">
        Thats all!
    </dsp:oparam>
 
    </dsp:droplet>
</dsp:page>

Step 3: To access this JSP type, http://localhost:8080/myapp/hello/helloemployee.jsp in the browser and see the output as following: