Skip to end of metadata
Go to start of metadata


JSP and Freemarker files are created by doing a new file in IDE. Instead of the '.jsp' extension, freemarker files are given a '.ftl' extension. The freemarker files are known as templates.

Nothing changes in regards to the controller returns. The Spring ModelAndView is created with the model objects and the view name as was done for JSP. The difference is the view resolver defined in the krad-servlet.xml uses the Spring FreeMarkerViewResolver and looks for the file as viewName.ftl.


The interpolation syntax between Freemarker and JSP is the same. They both use '${ }' syntax.

Referencing nested paths is again the same ("." notation), along with map and array notation.

The one notable difference is Freemarker can call a function:


Variable Assignment

In JSP we created variables using the c:set tag. In Freemarker, variables are created using the assign or local directives.

Using the assign macro creates a variable in the current namespace. The local directive is only available within macros and is used to create a variable with macro scope (doesn't impact any global variable with the same name).

Control Statements

Controls statements in JSP were mostly handled by use of the JSTL (c) tags. In Freemarker this are know as directives. All directives start with '<#' then a keyword.

Note in JSP the condition was given as a value for the test attribute (and needed quotes). In freemarker the condition just follows the keyword and does not need quotes.





The standard expressions are supported by both and are for the most part the same. The only notable difference is Freemarker does not support the alpha operator representation ('eq', 'ne', 'lt', 'or'). Operators are specified with the usual '==', '!=', '&&', '||' and so on. The only exception in Freemarker is for '>','<','>=','<=" operators, the alpha operator can be given as well.


JSTL provided utility functions through the fn: namespace. With Freemarker, utility methods are known as 'Built-Ins'. These are invoked by add '?' to the end of a variable (or expression) and then the build-in name.

The list of built-ins in Freemarker is much more extensive than functions available in JSTL:

Includes and Tags

To include another file (in Freemarker referred to as templates) Freemarker has an include directive similar to JSP.

The primary difference here is Freemarker simply grabs the file contents and processes as if the contents were inline, opposed to the extra overhead of JSP includes going through the servlet.

Like JSP, files brought in by an include share the same variable namespace (the global).

In JSP we created tag files to have reusable content that could accept named parameters, and be inserted into the page as a tag. Freemarker has a similar concept called macros.

Macros are created using the <#marco directive. Following the directive name is the name for the macro, then a list of parameters the macro can accept. Parameters can be given a default value by adding '=value' after the parameter name.


Macros are invoked in Freemarker by '<@' then the macro name, followed by parameter name/value pairs.


Freemarker also supports a type of macro known as a function. This is a macro that returns a value.


To include the tag body in the output of a macro, Freemarker has the <#nested> directive.

Spring Library

Spring provides Freemarker macros the have the same functionality as the Spring JSP tags.

Other Notes

  • Freemarker doesn't like null values. If JSP encountered a null value (for example for an interpolation or parameter value) it would simply output the empty string. Freemarker throws an exception. To tell Freemarker not to throw an exception but instead output an empty string, you must put '!' after the variable. This has to be done for each variable that could be null.
  • Similar to first time parsing of JSP files on application startup, there is a slowdown for the first time a Freemarker is read (although not as extreme). Freemarker then caches the templates for further use. It will periodically check for updates to the template and reload if changed. The amount of time between checks can be configured within the krad-servlet.xml (or the application spring module xml). For development purposes this should be low, and high for production.
  • Freemarker templates (including the templates containing macros) are not required to be present in any certain place (for example WEB-INF) and can be packaged with jars.
  • Freemarker has a <#compress> directive that will remove any extra whitespace from the output. This has been a problem in the past with JSP output. This is currently wrapping output from the KRAD UIF making the HTML source code much more readable.


Freemarker Documentation

Good article discussing benefits of Freemarker over JSP

Example Syntax Comparison:

  • No labels