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.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
    mavenCentral()
}
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.
book.ftl
<html>
<head><title> ${bookObj.name} </title>
<body>
<h1> ${bookObj.name} </h1>
<p>
  ${bookObj.bookDetail}
</p>
<h3>Writers</h3>
<#list bookObj.writers as writerName>
    ${writerName_index + 1} - ${writerName}  <br/>
</#list>
</body>
</html> 

Action Class

Find the annotation based action class in which @Result is using type="freemarker" as an attribute.
BookAction.java
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;
@Namespace("/freemarker")
@Action("/book")
@ResultPath(value="/")
@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.setName("Ramayan");;
    	  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;
	}
} 

Book.java
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;
	}
} 

web.xml

<web-app>
  <display-name>Struts 2 Freemarker</display-name>
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.concretepage.action</param-value>
        </init-param>
    </filter>
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app> 

Output

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

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us