ART#112 - How to create JSPs in ATG ? (using ATG's DSP tag library)



Now, before we jump into the syntax of ATG's very own (dsp) tag library, we must understand how a jsp functions.. 
Let us have a look at below diagram:-



1. You use the browser, type a URL (probably having some JSP) and send a request for a JSP to ATG application.

2. The ATG application gets the request for the JSP file you requested.

3. ATG application searches for the JSP in its module.

4. Once, the JSP is found, the whole JSP is compiled into JAVA code (unless it is pre-compiled of-course). So, whatever dynamic content you are trying to fetch using ATG's tag library or JSTL (yes, ATG supports JSTL too.. happy now..??), is fetched using JAVA code.

5. Now, when all the dynamic content(might be from the database) is fetched by the java code, the end result is converted into plain HTML.

6. This HTML page is sent back to the browser. You can see this "plain" html by seeing the source of the JSP you are seeing on your browser. This HTML would never have your dynamic tags you wrote in your JSP, as it has been converted into "plain HTML" by ATG.

Now we got our JSP basics clear.. lets move ahead..

Where to create your JSP?
We did this, when we created and invoked the "Hello World Droplet".
Let us revise the concepts once again..

Inside your ATG module, a "j2ee-apps" folder exists, inside which your application exists, which contains a "war" file (or war archive). All your JSPs should be placed inside this "war" file. You can create pretty much any number of nested folders and keep your JSPs there.

Just right click on any folder inside your .war file, click on "New" and click on "JSP". If you dont find "JSP" link, click on "Other", and then search and find "JSP" (i bet you might be knowing this much eclipse.. and if you dont.. this is not an eclipse tutorial anyway..)

Have a look at below screenshot:-



What to write in a JSP?

Now  that we know where to keep our JSP files, let us start writing...
Your JSP page should always always start with the below page directive:-

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

Remove all that crappy <!DOCTYPE> and <%@page> directives from your JSP.. and start with the above line.. 'coz this is the real s**t.

Alternatively, you can also start with 

<%@ taglib uri="http://www.atg.com/taglibs/daf/dspjspTaglib1_1" prefix="dsp"%>

instead of above page directive.
(You dont need to learn this.. just copy paste it somewhere and start dumping it in your JSPs).

Now that you have included ATG's dsp tag library, you can start using dsp tags right away.
Your page content should always be enclosed in <dsp:page></dsp:page> tags. Everything you write, MUST be enclosed in these tags.
(You can see Hello World Droplet section's "How to invoke a droplet" section to see the basic JSP structure).

Now, you can write all of your HTML code, your JSTL stuff and your droplet invocations and you dsp tags (which we are about to learn).. and.. duh! a lotta stuff basically..!

Although ATG supports JSTL tags, but it is recommended by ATG to use dsp tags, as they have enhanced functionality to their corresponding JSTL tags. For example, ability to pass objects is there in DSP but not in JSTL.


Basic dsp tags

1. <dsp:importbean>
When we created our hello world droplet, and invoked it, we gave the full config-path of the droplet, which was something like
<dsp:droplet name="/atg/hello/HelloWorldComponent>........ </dsp:droplet>

now, if we need to use this droplet say.. 10 times on a single page (yeah i am exaggerating a bit), we need to write the full config-path of the droplet right? WRONG!!

We can use a <dsp:importbean tag to serve our purpose. Let us see how:-

<%@ taglib uri="http://www.atg.com/taglibs/daf/dspjspTaglib1_1" prefix="dsp"%>
<dsp:importbean bean="/atg/hello/HelloWorldComponent" />
<dsp:page>
<dsp:droplet name="HelloWorldComponent>.......</dsp:droplet>
</dsp:page>

In above example, we used the <dsp:importbean> tag to import our component. Next, we used the <dsp:droplet> tag with simply the component's name rather than writing the full config path. This way, we can import the component once, and use it any number of times, just by using component name.

NOTE: Please note that, <dsp:importbean> tags should appear after the taglib directive. (See above)

For more details on <dsp:importbean> tag, you can refer to ATG's Page Developer's Guide and see this tag HERE.

2. <dsp:valueof>
This tag is used to display dynamic values. For example, dynamic value from a component, parameter, or simple an EL variable (Expression Language).
Examples:-

  • Below tag displays the value of a parameter accessible on this page.
  • <dsp:valueof param="[your parameter's name]"></dsp:valueof>

  • Below tag displays the value of a component's property.
  • <dsp:valueof bean="[your components path].[property-name]"></dsp:valueof>
    Please note that you can also use this tag in conjunction with <dsp:importbean> tag, so that you dont have to specify the full component path in this tag again and again.

  • This would display the value of any EL variable on this page.
  • <dsp:valueof value="[your EL variable]"></dsp:value9Tof>

  • <dsp:valueof> tags can also have a default value in between. If the value of param, component's property or EL variable cannot be printed, default value is printed on the JSP.
  • <dsp:valueof param="[your parameter's name]">Default Value</dsp:valueof>


    You can see more details on this tag in ATG's Page Developer's Guide HERE.
3. <dsp:getvalueof>
Consider a situation, wherein you get a request parameter from a droplet, and you want it to be visible through the session?
OR you want to hardcode a specific string into the whole application (irrespective of session.. as in global-iSH)
OR you want the property value of a component to persist in a session (or probably globally?). We do not know how to do that.. but soon.. we will..!!

<dsp:valueof> tag enables to to get a page-parameter/component's property or some static value (or literal) to be accessible in a variable (which, in future can be used as an EL variable). Now, you know... variables have scopes you see! This scope can be set as one of the following:-


  • page : EL-variable access is limited to page. (This is default)
  • request: EL-variable access is limited to request.
  • session: EL-variable access is present in the whole session.
  • application: EL-variable access is present for the whole application to use, anywhere in/outside a session.
If you dont specify any scope, it is page. 
You can see more about scopes in ATG's page developer's guide HERE.
Let us see the tag now and understand how we can get the values into scopes..

Example:

<dsp:getvalueof param="[your parameter's name]" var="[your EL variable name]" scope="[any of the above scopes mentioned]"></dsp:getvalueof>

Just like the above <dsp:valueof> tag, param can be replaced by "bean=" or "value=" for using component and static values respectively. The value of this field will be copied to the variable you mention in the "var=" attribute. "scope=" attribute is optional (which means "page" scope, if you skip this atribute), but can be used to change the scope of the EL-variable (NOT the param/component-value).
This EL variable can be used in standard JSTL way or by using <dsp:valueof tags>

For example, if we consider our component "Person" specified in ART#104, and display its property "age", we use the following JSP code, which depicts multiple ways of displaying:-


1. <%@ taglib uri="http://www.atg.com/taglibs/daf/dspjspTaglib1_1" prefix="dsp"%>
2. <dsp:importbean bean="/atg/persons/Person" />
3. <dsp:page>
4. <dsp:getvalueof bean="Person" var="personVariable" scope="session"></dsp:getvalueof>
5. Using JSTL tag: <br />
6. <c:out value="${personVariable.age}"/><br />
7. Using EL variable: <br />
8. ${Person.age} <br />
9. Using dsp tag: <br />
10. <dsp:valueof value="${personVariable.age}"></dsp:valueof>
11. </dsp:page>


At step 2, we import the component "Person".
Next, at step 4, we use the <dsp:getvalueof> tag, put the component inside the EL variable "personVariable". Also, we set the scope of this EL variable as session, therefore, we can use "personVariable" throughout the session on any page.
At step 6, we use the general JSTL <c:out tag to print the EL variable
At step 8, we simple print the EL variable using the EL syntax.
At step 10, we use the dsp:valueof tag to print the age.

For more details on this tag, you can also refer to Oracle's page developer's guide, and see the dsp:valueof tag HERE.

4. <dsp:setvalue>
This tag is used when you want to set a component's property OR a page parameter from:-

  • A component's property value <dsp:setvalue bean="/atg/persons/Person.age" beanvalue="[another component's nucleus path].[property-name]" />

  • A parameter's value (Setting component's property using a parameter's value)<dsp:setvalue bean="/atg/persons/Person.age" paramvalue="[your parameter's name]" />

  • A static value (Setting component's property using a static value)
    <dsp:setvalue 
    bean="/atg/persons/Person.age" value="21" />
<dsp:setvalue bean="[your component path].[property-name]"
can be replaced by 
<dsp:setvalue param="[your param name]"

 when needed.
The tag basically has the target first, and source later.
<dsp:setvalue [TO] [FROM]  />

More details on this tag can be found in ATG's page developer's guide HERE.

These tags should get you going now, for further details you can refer to ATG's page developer's guide for more details HERE as there is still a lot of inner details in JSPs you might not know..!
Although nobody asks JSPs in ATG interviews, and you have a nice documentation to refer to, you must know what the basic tags mentioned above do, and when to use them...

Now that we have covered basic dsp tags, let us move on to the last article of this "Basics" chapter, which is FormHandlers..!
We will move on to a much higher level when we complete this chapter..!


Back




Next





10 comments:

  1. what is diff b/w item-type and component-item-type? briefly explain with examples?

    ReplyDelete
    Replies
    1. 1.item-type: When your item-descriptor refers to another item-descriptor in a one-to-one relationship, you use item-type. For example:-
      Consider the "user" item-descriptor. You have a requirement of having a "defaultAddress" for a particular user. In this case, each user would always have a single default address. This is a one-to-one relationship.
      Address corresponds to the out-of-the-box "contactInfo item-descriptor. Therefore, your "user" item-descriptor should have property something like:-

      < item-descriptor name="user" xml-combine="append" >
      < table >
      < property name="defaultAddress" item-type="contactInfo"....... />
      < /table >
      < /item-descriptor >

      In "user" item-descriptor, there is an out-of-the-box property called "homeAddress". You can refer to this for "item-type" reference XML.

      2. component-item-type: This is used mostly for one-to-many or many-to-many relationships.
      Now, suppose you have a requirement, wherein a user can have multiple addresses. This would be a one to many relationship. So, your "user" item-descriptor can have "allAddresses" property which would refer to a list (or set) of many "contactInfo" items. In this case, you would use "component-item-type" instead of "item-type" in conjunction with a "data-type" attribute which would tell the repository that this is a "set" or a "list" of "contactInfo" items.
      Considering this scenario your property would look something like:-

      < item-descriptor name="user" xml-combine="append" >
      < table....... >
      < property name="allAddresses" component-item-type="contactInfo" data-type="set"......./ >
      < /table >
      < /item-descriptor >

      In "user" item-descriptor, there is an out-of-the-box property called "allSecondaryAddresses". You can refer to this for "component-item-type" reference XML.

      Please let us know in case of any more doubts.

      Thanks,
      Monis

      Delete
    2. can you explain item and item desciptor please

      Delete
    3. An item-descriptor is like a set of properties (similar to a class).
      An item is like an object which has values for those properties. Therefore, using an item-descriptor we can create multiple objects which have different values of properties (which are defined by item-descriptor).

      Delete
    4. Also, there are more details HERE:-
      http://learnoracleatg.blogspot.in/2014/11/art201-what-is-repository-in-atg.html

      Delete
  2. Hi Monis. Thankyou for writing this blog. Its so useful for me. I have few queries, and one among them is
    I found dsp:getvalueof is used in two ways

    -> and the var is used as EL later

    and the id is used as scripting variable later.

    What is the difference in the usage of both?

    ReplyDelete
    Replies
    1. I guess you are referring to the component properties usage using tag.
      This tag is used, when you want to copy the value of a parameter or a bean into a variable.
      In our example,


      Here, "Person" is a bean and it has some property values [like id, age, name etc.]
      Next, we have the "personVariable" as a variable, which can be used to fetch individual properties of the "Person" bean using the "dot(.)" operator.

      Therefore, we can use the "." operator to fetch the properties of "Person" bean using any of the syntax like:-
      1. EL Variable
      2. tag [JSTL]
      3. tag [DSP tag library]

      Hope this answers your question.

      Delete
  3. Thanks For writing this blog,But It would be great if you an tell the difference betwwen valueof and getvalueof tag..



    ReplyDelete
    Replies
    1. valueof - used to DISPLAYING the value of EL variable, PARAM, or COMPONENT's property value.

      getvalueof - used for putting the value of PARAM or COMPONENT's property value into an EL variable. You can also define the scope of this EL variable to various scopes (pageScope, applicationScope etc.)

      Delete
  4. Thanks for your tutorials! They're really great!
    I'm not sure, it might be a typo:

    <dsp:valueof> tag enables to to get

    should it be <dsp:getvalueof> ... ?

    ReplyDelete

Subscribe

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!

Flickr