Thursday, December 15, 2011

ATG Tag Library:DSP Tags

There are many ATG Out Of The Box(OOTB) droplets to cater the various needs in developing an application.Droplet’s main use in a jsp page is to dispay the contents dynamically. The OOTBox components are used in JSP with the help of DSP tags. DSP is a tag library provided by ATG, it acts as layer above the JSP tags. The DSP tag library lets you access all data types in ATG’s Nucleus framework. Other functions provided by these tags are managing transactions and rendering data in a Java Server Page. Tags from the DSP tag library are used only for tasks that involve Dynamo Application Framework (DAF) resources.
In the API documentation of ATG the class hierarchy is: atg.nucleus.GenericService —> atg.servlet.DynamoServlet. And the classes for sample droplets “Switch” and “ForEach” droplets are “atg.droplet.Switch” and “atg.droplet.ForEach” respectively. Before using the DSP tags in a page, it must be imported in the beginning of the page.

<%@ taglib uri=”/dspTaglib” prefix=”dsp”%>

1.DSP PAGE tag:
The dsp:page surrounds the content body of the JSP. It is required to use this tag as such whenever your pages include DSP tags.”dsp:page” tag initiates the page rendering process by creating an instance of the “DynamoHTTPServletRequest”. This tag is preceded only by the page directives and import statements.
["content of the page.It can be any dsp or jsp or html tags"]


2.DSP IMPORTBEAN tag:
dsp:importbean can import a servlet or java bean component into a JSP.It is usually given at the top of the page so that it facilitates reference to all other elements.The tag has 3 attributes BEAN,VAR and SCOPE, of which bean is a compulsory attribute and can’t be omitted.The bean attribute accepts a fully qualified nucleuspath and name of the servlet bean that we want to import. The VAR attribute names an EL variable so that it can be used by other tags in the page. If SCOPE is not mentioned a default page is used.SCOPE determines where the servlet bean specified by VAR is stored and who all can access it.PAGE,REQUEST,SESSION & APPLICATION are the various options available. Now a small eg: will serve a better understanding.

Sample Code:

<dsp:importbean bean="/atg/dynamo/droplet/Switch"/> 
<dsp:importbean bean="/atg/dynamo/servlet/RequestLocale" var="requestlocale"/>  <dsp:importbean bean="/atg/dynamo/droplet/Redirect/> <dsp:setvalue bean="${requestLocale.refresh}" value=" "/>  
<dsp:droplet name="Switch">   
  <dsp:param bean="${requestLocale.locale.language}" name="value"/>  
  <dsp:oparam name="fr">   
      <dsp:droplet name="Redirect">  
        <dsp:param name="url" value="fr/index.jsp"/>  
      </dsp:droplet>  
</dsp:oparam>  
  <dsp:oparam name="default">  
      <dsp:droplet name="Redirect">  
         <dsp:param name="url" value="en/index.jsp"/>   
      </dsp:droplet>   
</dsp:oparam>   
</dsp:droplet>


Here the Switch, RequestLocale and Redirect droplets are imported to the page. The RequestLocale component is imported to a VAR attribute so that the dsp:setvalue and dsp:param tags can access RequestLocale with EL. EL is enumerated language so it requires the EL tag libraries as well(JSTL).


3.Usage of bean variables in dsp page?
Before we deep dive, lets have a look at the various shards we are clubbing together.First we will create our miniscule component, lets name it “SampleBean“(SampleBean.properties) ofcourse the twin java class is there, lets name it “SampleBeanJ“(SampleBeanJ.java), the “J” can be used to differentiate the property file from a java file.The other important piece in the story is the jsp file, which we will call “ttt“(ttt.jsp), the three T’s represent three2tango.







a. SampleBean.properties

The component which bridges the java file with the jsp file.This name is used to import the java class into the jsp. In our example we are keeping it simple, nothing abstruse will be written in that.
$class= rootfolder.SampleBeanJ
The “rootfolder” is the location where the java class resides.And its in this SampleBeanJ we will write our actual application logic.

b. SampleBeanJ.java
SBJ is written like any other java class, it starts with the package information, then the imports, then the class declaration with access specifiers and extentions if any.

 package rootfolder; 

 import java.io.IOException;

   public class SampleBeanJ 
   { 
       private String varFromBean; 
    
       //getter method 
       public String getVarFromBean() { 
           return varFromBean; 
       } 
       //setter method. 
       public void setVarFromBean(String varFromBean) { 
           this.varFromBean = varFromBean; 
       } 
   }  


How the variable “varFromBean” is getting/setting a value is a different issue and we will discuss it in some other session. Our prime focus is on how to use a bean variable in a jsp, also the various methods of handling the bean variables.
Lets take a look at our “ttt.jsp”
Dont forget to include the dsp tag libs at the very beginning. Also the tag libs for the jstl(Jsp Standard Tag Library).

c. ttt.jsp

   <%@ taglib uri="/dspTaglib" prefix="dsp" %> 
   <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>  


   <dsp:page> 
   <dsp:importbean bean="rootfolder/SampleBean"/>  


 
  //comment : the attribute bean specifies the property file, which in turn points to the java file.(The ATG component centric architecture)  

   <% 
   String jspVar1 = "foo"; 
   //comment : Java code inside the JSP is kept inside <% and %> characters (just like expressions, but without the = sign at the start of the sequence.)This block of code is known as a "SCRIPLET".A scriptlet contains Java code that is executed every time the JSP is invoked. 
   %>  


   <dsp:getvalueof bean="SampleBean.varFromBean" idtype="java.lang.String" id="jspid">  


   <% 

   jspVar1 = jspid; 
   //comment: the "jspid" has the value of the private string variable"varFromBean" in the  

     SampleBean class. 
   %>  


   </dsp:getvalueof>  


   <%

     if("foo".equals(jspVar1)){
   %> 
   <c:out value="foo from the jsp itself"></c:out>  


   <%}else{%>  


   <dsp:valueof bean="SampleBean.varFromBean"/>  


  
//comment:value of the variable will be printed. 
   <%}%> 
   </dsp:page>  



Here we see a method of using the variable in a bean inside the jsp.There are different other ways also to do the same. I will try to explain those in other posts. Hope this one helps.

3 comments:

Aniket said...

Nice Post.
The last code, using a scriplet in JSP file, seems very unfriendly.
Thanks.

The below is also a nice approach.
http://blog.chomperstomp.com/atg-droplets-and-serving-a-default-oparam/

Regards,
ATG developer & freelance web developer Mumbai

Unknown said...
This comment has been removed by the author.
Unknown said...

Thank you Ankit, Now you can easily understand what exactly these scriplets are doing, we know that using scriplets is not a good practice, i just want you to understand it clearly, for that i have included it, thank you for suggesting that blog, as you said its really a nice blog.

Popular Posts