Home  >  Struts 2

Struts 2 + Freemarker Template Annotation Integration Example

By Arvind Rai, October 15, 2015
This page will provide Struts 2 and Freemarker template annotation integration example. To get started with it, we need to include Freemarker JAR dependency in our Struts 2 project. To enable Freemarker in struts 2, we need to use type="freemarker" as an attribute of @Result annotation and location attribute will represent .ftl file. This will look as follows.
@Result(name="success", type="freemarker", location="book.ftl")
` Here on this page, I will provide a complete example of Struts 2 and Freemarker integration example step by step. To understand basic Freemarker example, find the link given below.

Java Freemarker Templates Example

Software used in Demo

Find the software being used in our demo.
1. Java 8
2. Tomcat 8
3. Struts 2
4. Freemarker 2.3.23
5. Gradle
6. Eclipse

Gradle to Resolve JAR Dependencies of Struts 2 and Freemarker

Find the build.gradle to resolve the JAR dependencies of Struts 2 and Freemarker.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
dependencies {
    compile 'org.apache.struts:struts2-core:2.3.16'
    compile 'org.apache.struts:struts2-convention-plugin:2.3.8'    
    compile 'org.freemarker:freemarker:2.3.23'

Project Structure in Eclipse

Find the project structure in eclipse.
Struts 2 + Freemarker Template Annotation Integration Example

Freemarker Template

For the demo, we have a sample Freemarker template in which we are showing title, body message and iterating list.
<head><title> ${bookObj.name} </title>
<h1> ${bookObj.name} </h1>
<#list bookObj.writers as writerName>
    ${writerName_index + 1} - ${writerName}  <br/>

Action Class

Find the annotation based action class in which @Result is using type="freemarker" as an attribute.
package com.concretepage.action;
import java.util.Arrays;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.concretepage.bean.Book;
import com.opensymphony.xwork2.ActionSupport;
@Result(name="success", type="freemarker", location="book.ftl")
public class BookAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private Book bookObj = new Book();
    	  bookObj.setBookDetail("This is holy book.  <br/>Ram and Ravan fights each other and Ram wins.");
    	  bookObj.setWriters(Arrays.asList("Valmiki", "Tulasidas", "Ramanand Sagar"));
	public String execute() {
	        return SUCCESS; 
	public Book getBookObj() {
		return bookObj;
	public void setBookObj(Book bookObj) {
		this.bookObj = bookObj;

package com.concretepage.bean;
import java.util.List;
public class Book {
	private String name;
	private String bookDetail;
	private List<String> writers;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public String getBookDetail() {
		return bookDetail;
	public void setBookDetail(String bookDetail) {
		this.bookDetail = bookDetail;
	public List<String> getWriters() {
		return writers;
	public void setWriters(List<String> writers) {
		this.writers = writers;


  <display-name>Struts 2 Freemarker</display-name>


Find the URL to test the application http://localhost:8080/concretepage-1/freemarker/book . We will get output as given below.
Struts 2 + Freemarker Template Annotation Integration Example

Download Source Code


©2022 concretepage.com | Privacy Policy | Contact Us