Pages

Tuesday, September 24, 2013

AEM/CQ5 SQL2 How do I find all occurrences of a node by name

The Problem

You want to use a SQL2 query to retrieve all nodes that are named "nameofnode"

The Solution

SELECT * FROM [nt:base] AS s WHERE LOCALNAME() LIKE "nameofnode"

The LOCALNAME() function returns the node name. 

Tuesday, June 4, 2013

How to create your own sling adapter

The Problem

You want to create your own implementation of an adaptTo() class

The Solution

Coming Soon

CQ5/AEM What possibilities exist with adaptTo?

The Problem

You want to find out what you can adapt a particular object to, using the adaptTo(Object.class) method.

The Solution

Go to http://localhost:4502/system/console/adapters and have a look for the class in the first column, Adaptable Class.

The next column provides all of the classes that you can adapt the originating class to, any conditions that may be imposed, and the bundle that provides the information.

So for example, if you look up org.apache.sling.api.resource.Resource you will see that one of the listed adaptable classes is com.day.cq.wcm.api.Page

Thus, if you start with a Resource resource, you can do:
Page page = resource.adaptTo(Page.class)

You could even create an adaption of your own. For example, if you have a Node object, you can create an AdaptTo mapping to map to MyCustomNode.

Further items of interest

How to create your own sling adapter.

Tuesday, May 28, 2013

CQ5/AEM how to create and use a custom taglib - part 3 use your tag

If you have not already completed part II, click here

The final step is to use your tag.

In the component of your choice, add the following to a jspx file:


<?xml version="1.0" encoding="UTF-8"?>
<jsp:root xmlns="http://www.w3.org/1999/xhtml"
          xmlns:jsp="http://java.sun.com/JSP/Page"
          xmlns:btes="http://btes.com.au/tagexample"
          version="2.1">
    <jsp:directive.include file="/apps/cqblueprints-example/components/global.jspx"/>
    <p>No tag left behind!</p>
    <btes:customTag content="Custom tag engaged" isHeading="true"/>
</jsp:root>

Now you should see the text "Custom tag engaged" rendered out as a heading. If you change the isHeading boolean to false, you should see it render as a paragraph.

Note that code complete should be fully functional with this example.

CQ5/AEM how to create and use a custom taglib - part 2 create and set up your TLD

If you have not already completed part I, click here.

The next step is to created your .tld directory.

In the taglib project, you will see under src/main/resources, that you have a META-INF directory.

In this directory, create a file named ProjectTaglib.tld

Add the following to this file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1">
    <tlib-version>5.4.0-SNAPSHOT</tlib-version>
    <short-name>btes</short-name>
    <uri>http://btes.com.au/tagexample</uri>
    <tag>
        <name>customTag</name>
        <tag-class>au.com.btes.tagexample.CustomTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
  <name>content</name>
  <required>true</required>
</attribute>
<attribute>
  <name>isHeading</name>
  <required>true</required>
</attribute>
    </tag>
</taglib>

For Part III, click here



CQ5/AEM how to create and use a custom taglib - part 1 create a custom tag.

The Problem

You want to create a custom taglib, and to have code complete in your target JSP file.

The Solution

In your taglib project, Add a new file called TestHeading.java, and include the following code:



package au.com.btes.tagexample;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import com.cqblueprints.taglib.CqSimpleTagSupport;

/**
 * Example custom tag to show either content wrapped within a heading, or content wrapped within
 * a paragraph
 * @author bportier
 *
 */
public class CustomTag extends CqSimpleTagSupport {
private String content;
private boolean isHeading;

@Override
public void doTag() throws JspException, IOException {
String tagString="";

if(isHeading) {
tagString="h2";
}
else {
tagString="p";
}
String message="";
message=message.concat("<").concat(tagString).concat(">");
message=message.concat(content);
message=message.concat("</").concat(tagString).concat(">");
getJspWriter().write(message);
}

public String getContent(){
return content;
}

/**
* Defines the content that the tag will display
* @param content
*/
public void setContent(String content){
this.content=content;
}

public boolean getIsHeading(){
return isHeading;
}

/**
* Defines whether to display a heading (true) or a paragraph (false)
* @param isHeading
*/
public void setIsHeading(boolean isHeading){
this.isHeading=isHeading;
}
}



This has now created your custom tag.



CQ5/AEM how to get JSTL Code complete

The Problem

After including <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> into your JSP, there is still no code complete for JSTL code, such as <c:set> or <c:out>

The Solution

You need to have the JSTL standard.jar library included in your project libraries.

If you are not using maven, you can download the 1.2 JSTL package from http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/

If you are using maven, include the following in your relevant pom files:


<dependencies>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>