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="" xmlns:xsi=""
 <description><![CDATA["To provide convenient helpers for web elements."]]></description>
 <display-name>Muse Web Tags</display-name>

   <description><![CDATA[The list containing the actual data]]></description>


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="" %>
    <muse:pagination list="searchResult" />

2 thoughts 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?


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s