How to Create a Struts 2 Component with Freemarker

For quick starter, you can use tag file to create a reusable component for your JSP application. However, if you wants a more serious tag, or need to use Struts’ value stack ability to lookup action variable, it is better to create  a Struts component.

The steps are simple:

That’s all! This is tested on Struts 2.3.x.

Create a Component

The Struts 2 component is the core of everything.

public class Pagination extends UIBean {
  protected Object list;

  protected void evaluateExtraParams() {
    addParameter("list", findValue(list));
  }

  public void setList(Object list) {
    this.list = list;
  }
}

Create a Tag

public class PaginationTag extends AbstractUITag {
  private String list;

  public void setList(String list) {
    this.list = list;
  }
}

Create the TLD File

This enables you to use the tag in the JSP file.


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 version="2.0"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
 <description><![CDATA["To provide convenient helpers for web elements."]]></description>
 <display-name>Muse Web Tags</display-name>
 <tlib-version>2.3</tlib-version>
 <short-name>muse</short-name>
 <uri>http://www.museviral.com/museweb-tags</uri>

 <tag>
  <name>pagination</name>
  <tag-class>com.museviral.snstrade.web.struts2.views.jsp.ui.PaginationTag</tag-class>
  <body-content>JSP</body-content>
  <attribute>
   <description><![CDATA[The list containing the actual data]]></description>
   <name>list</name>
   <required>true</required>
   <rtexprvalue>false</rtexprvalue>
  </attribute>
 </tag>

</taglib>

Create a Freemarker Template

I found that everything is OK when your freemarker template file has the same name as your tag name e.g. pagination  here

So, we create the following file


hello world! ${parameters.list]

Use It In Your JSP

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="muse" uri="http://www.museviral.com/museweb-tags" %>
<html>
  <body>
    <title>Demo</title>
  </body>
  <head>
    <muse:pagination list="searchResult" />
  </head>
</html>
Advertisements

One thought on “How to Create a Struts 2 Component with Freemarker

  1. Hy, thanks for your post, I’m still having difficulty to create a custom tag, Can please add complete source of the example ora a .war file?

    Thanks,
    Giorgio

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s