SmartTarget 2014 SP1 and DD4T in a Java environment

As Nick Roussakov explains in his excellent post, it is very easy to integrate DD4T and SmartTarget 2014 – at least, no harder than it is to implement SmartTarget on any type of system.

If you are implementing the java version of DD4T things are even easier. First of all, you can simply add the out of the box Add to SmartTarget TBB to a dynamic DD4T component template. This will make sure that the component presentation is available in FredHopper – as described by Nick.

On the java side, you can re-use all of the SmartTarget tags in your JSP views. In that light the life of a DD4T/java developer is even easier than that of his .NET counterpart.

The following code is taken from Jan Horsman’s very helpful code snippet for using¬†ST in a jsp (not with DD4T):

<smarttarget:query siteEditTagName="span" publication="tcm:0-72-1">
   <smarttarget:promotions region="Homepage Banners" var="promotion"  
      maxItems="1"> 
		      <smarttarget:itemTemplate> 
			         <smarttarget:promotionalItems> 
				            <smarttarget:itemTemplate>
               <tridion:ComponentPresentation pageURI="tcm:72-6212-64"
                  componentURI="${item.componentUri}"
                  templateURI="${item.templateUri}"/>
            </smarttarget:itemTemplate> 
         			</smarttarget:promotionalItems> 
      		</smarttarget:itemTemplate> 
   	</smarttarget:promotions> 
</smarttarget:query>

The color coding is mine. The purple sections are part of SmartTarget. They are responsible for creating a query, sending it to FredHopper and iterating over the results. The results consist of two values: a component URI and a component template URI.

These URIs are then used to load a dynamic component presentation from the broker (shown in black). This is the part that must be modified for DD4T. If you don’t, this code will output the DD4T XML directly in the web page – not what we want!

In my DD4T project, I created a tag which uses a view to render the DD4T component. The code looks like this:

<smarttarget:query siteEditTagName="span" publication="tcm:0-72-1">
   <smarttarget:promotions region="Homepage Banners" var="promotion"  
      maxItems="1"> 
		      <smarttarget:itemTemplate> 
			         <smarttarget:promotionalItems> 
				            <smarttarget:itemTemplate>
               <my:dynamicComponentPresentation component="${item.componentUri}" view="smarttarget-content-item" />
            </smarttarget:itemTemplate> 
         			</smarttarget:promotionalItems> 
      		</smarttarget:itemTemplate> 
   	</smarttarget:promotions> 
</smarttarget:query>

The tag ‘dynamicComponentPresentation’ must be included in a *.tld file inside the WEB-INF/tld folder in the webapp. The definition looks like this:

<tag>
    <name>dynamicComponentPresentation</name>
    <tag-class>com.myorg.tags.DynamicComponentPresentationTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <name>component</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        <type>java.lang.String</type>
    </attribute>
    <attribute>
        <name>view</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        <type>java.lang.String</type>
    </attribute>
</tag>

The code of the DynamicComponentPresentationTag and a util class (RenderUtils) are included as a download. The code assumes you have a ComponentController mapped to the url /VIEWNAME/COMPONENT_URI.dcp (if you use a different way to render component presentations, you need to change the RenderUtils class).

Views and component templates

The only downside of this approach is that you must hardcode the name of the view with which to render the component. This works fine as long as you have only one type of DCP in your promotions.

The problem will be solved when DD4T comes out, which will have a ComponentPresentationFactory rather than a ComponentFactory. In that case, you can simply pass a component template URI to the controller. This controller will retrieve the entire component presentation, including information about the view.

DD4T 2.0 is scheduled for Q1 of 2015.

Happy DD4T’ing!

 

Download: