Monday, 24 March 2014

Including a Custom Tag within a Custom Tag File (tagx) using xml notation

Decided to write this up for reference as I had to go through the oracle java documents to finally find a solution. While there were solutions available, most of it concentrated on the tag file in a JSP document/xml notation. This blog can for a basis/example for writing a custom tag file (using the xml notation) rather than the java notation.

It all started with writing a tag file for creating a dropdown using an enum, which then I had to include in another custom tag for creating a table.

The first part was simple enough (though found few samples, could be due to the simplicity associated with creating a tag file), nevertheless I had to really struggle to get the things in order.

Step 1:
Decide the tag Library Name, in this case lets name it formtags.
Create a folder tags under the WEB-INF folder, this step is mandatory. Once can create any number of subfolders under this with the name of their choosing and then decide to include the same. This will help them better organize their tags.

Step 2:
Create a file with extension "tagx". Using the tagx instead of tag will ensure that your tag follows a well formed xml notation thus ensuring a better readability, hence decided to use this even though if you do a search for tag files there will be lots of examples for using the .tag but bery few for the document form of custom tags.
Add the jsp root element to the file thus ensuring that most the jsp and jstl core libraries are available.

<jsp:root xmlns:c=""
 xmlns:form="" version="2.0">

Now as you can see I have included the spring tags too, as I am using this with a spring application and this gives me the all powerful option to manipulate the java using el.
You can see the use of the spring el in the example below, where we would be touching the tip of the uses where this can be put into.

Step 3:
Define the attributes required by the custom tag.

<jsp:directive.attribute name="listEnumName" type="java.lang.String"
 required="true" description="Fully qualified Name of the Enum" />
<jsp:directive.attribute name="path" type="java.lang.String"
 required="true" description="Select Model Field Name" />

As you can see I have defined the two attributes namely
1) listEnumName -- This will be used to pass the name of the enum which would be used to form the options for the drop down to be created dynamically.
2) path -- Used the attribute Name path as it would be used to pass on the path variable into the form tag.

Step 4:
Get the list of values available in the Enum.

<spring:eval var="enumValList"
 expression="T(${ listEnumName }).values() " />

Note the use of T which indicates to the Spring el that the listEnumName has to be cast to a type and then execute the values method on the type. The list is then assigned to enumValList.

The Enum that I have used for reference (In the example below the enum implements EntityEnum which has been added by me just to ensure that i implement a few methods, the el should work even without it.

public enum SortTypeEnum implements EntityEnum<SortTypeEnum>{

 private String value;
 private SortTypeEnum(String value) {
  this.value = value;
 public String getValue() {
  return this.value;

 public SortTypeEnum getEnumByValue(String enumValue) {
  for (SortTypeEnum fieldNameVal : SortTypeEnum.values()) {
   if (enumValue.equals(fieldNameVal.getValue())) {
    return fieldNameVal;
  return null;

public interface EntityEnum<T extends Enum<T>> {

 public String getValue();
 public T getEnumByValue(String enumValue);

Step 5:Calling the Custom tag from within another tag. This is where, I had to struggle to include the tag directory and finally was able to find the solution after reading through this "Declaring Tag Libraries" .
Once known it was a simple affair to call the custom tag from within another custom tag.
 Providing below a sample for reference, the trick here is to include the namespace as a urn.
urn:jsptagdir:&lt;tag directory path&gt;

<jsp:root xmlns:c=""
 <formtags:EnumSelect listEnumName="com.july11.framework.common.commonenum.SortTypeEnum" path="defaultSort"/>

One can use the normal taglibs also by using "urn:jsptld:&lt;Tld Path" instead of the jsptagdir.

No comments:

Post a Comment