Showing posts with label FreeMarker. Show all posts
Showing posts with label FreeMarker. Show all posts

Thursday, January 13, 2011

Iterate over JAVA HashMap in FreeMarker

Java HashMap can be iterated in FreeMarker by using a <#list> tag as follows:

<#list hashMap?keys as key>
        ${key}=${hashMap[key]}
<#list>


If you are using the above code with default FreeMarker Configuration then there would be some different behavior during iteration. For example if the HashMap is:

HashMap hashMap = new HashMap();
hashMap.put("1","one");
hashMap.put("2","two"); 

By iterating on hashMap with default Configuration it will output following keys in result:

put
remove
entrySet
hashCode
productElement
clear
isEmpty
1
values
empty
underlying
productElements
copy
getClass
get
copy$default$1
equals
productPrefix
class
canEqual
keySet
size
2
containsKey
productArity
containsValue
productIterator
toString
putAll        


This result shows that the iteration is done on all the available hashMap properties.

To get rid of this you have to set the FreeMarker Configuration as follows:


Configuration configuration = new Configuration();
BeansWrapper wrapper = (BeansWrapper) this.configuration.getObjectWrapper();
wrapper.setSimpleMapWrapper(true);
this.configuration.setObjectWrapper(wrapper);

Here the BeanWrapper's simpleMapWrapper property is set as 'true'. After setting this configuration the results over iterating on hashMap will give only the defined key-value pair.

rizzz86

Sunday, October 18, 2009

FreeMarker: Functions & Macros

FreeMarker allows you to define Functions and Macros in ftl's as well.

Functions <#function>:

Syntax:

  • In FM you can define a function using a simple syntax as follows:
<#function functionName param1 param2>

  • Here "functionName" is the name of a function and param* shows the parameters that can be passed into the function
Calling Function:
  • To call a function in ftl's following syntax is used: (Consider the same function definition as define in the 'Syntax')
${functionName(param1Val, param2Val)}


<#return>:
  • The <#return> directive is not mandatory in functions. You can use <#return> any where in the function. Calling a <#return> exits from a function. If no such directive is used it will executes the whole function.

Macros <#macro>:

Syntax:
  • Syntax for <#macro> is almost same as <#function>. The name & parameters of macro are define in the same way as in the functions:
<#macro macroName param1 param2>


Calling Macro:
  • Calling a <#macro> is done by using "@" directive. Macro is itself a user-defined directive.
<@macroName param1=param1Val param2=param2Val/>

Recursion:
  • Macros can perform recursion (calling Macro inside Macro).
  • The recursion is same as done in normal functions.
  • Recursion Syntax:
<#macro macroName param1 param2>

<@macroName param1=param1Val param2=param2Val/>

< /#macro>


rizzz86

Saturday, October 10, 2009

Development Using FreeMarker

If you are developing your application using freemarker, it is necessary for you to learn its syntax - to create FM templates.

I have faced some issues while building templates using FM and also learned the right way to use freemarker syntax.

Following are some points that I have go through while using freemarker:

Passing Beans to Templates instead of Lists:

In my case it is suitable for be to create a Java Bean and pass its object to templates. I was sending my data in the form of Lists previously but it is causing some problems as well as making things complex.

The structure in which the data is stored in the list is the biggest issue. What I have to do is to create a menu that can have n-numbers of sub-menus and each sub-menu can have n-numbers of their sub-menus. Getting really mixed up using lists. Now I have created a bean to handle my menus. Bean looks like as follows:



class MenuBean {

String title;
String url;
List children;
etc..
}



If condition and null object identification:

To handle conditional statements it is quite easy in freemarker. The "IF" condition can easily be applied any where with <#if> clause. One thing to remember here is that the closing tags are must for most of freemarker tags. So when the conditional tag ends also has to placed. Just like brackets open and closed.

Identification of empty list or null can also if done by <#if> tag. The syntax is a bit cheeky but it works. The statement "<#if yourList??>" verifies that their exists some element inside "yourList" and is not null, so it enter into the condition.


foreach tag:

Iterating through a list can also be done in freemarker. A simple <#foreach> tag is provided to iterate a list.

The sytax for foreach tag is also simple:

<#foreach element in yourList>


Here element is just an alias of your list that can be used to access the properties or values inside youList.

yourList is the name of list.


list tag:

List tag is also used to iterate any list. It can also be used as a "for loop".

To iterate a list using <#list> following syntax is used:

<#list yourList as x>


If you want to used <#list> to work as a for loop, you have to use it as follows:

<#list 1..10 as x>


The <#list> will loop from 1 to 10 and ends.


Click this Link for FreeMarker #Functions and #Macros


rizzz86

Tuesday, September 29, 2009

Wasted whole Day in FreeMaarker Issue

Yesterday I was trying to configure FreeMarker and CustomTags in a single application. No problems in CustomTags but I have wasted my whole day in getting the FreeMarker's Template path.

I was constantly getting the following error "javax.servlet.ServletException: Error:IOException while writing to clientTemplate firstftl.ftl not found." and the day ends. That was really frustating for me ;(

Some of my colleagues also tried their luck, but in vain !. Before leaving my desk at the day end I have posted this issue on FreeMarker Forum and left.

Today I have got the hint from that forum about the Templates Classpath to be set somewhere in properties file or xml's. After googling a bit I have found that I forgot to set the Template Path property in FreeMarker Configuration.

Its working fine now ... started my day with a little success :)

rizzz86