Today we will create a Spring MVC based application that uses Freemarker FTL as view instead of JSP. This would give you a good insight in Spring MVC + Freemarker integration. The application is similar to previous tutorial’s User app, where a list of users will be displayed and also we can add new user.
The application is very simple:
- There is a table that displays user info like firstname, lastname.
- New user can be added via Add User form.
Below is the wireframe of our final freemarker based spring mvc 3 app.
So lets get started.
Things We Need
Before we starts with our Spring MVC 3 + FreeMarker example, we will need few tools.
- JDK 1.6 or above (download)
- Tomcat 6.x or above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
- Eclipse 3.4.x or above (download)
- Spring MVC 3.1.2 or above (download)
- Freemarker JAR v2.3.15 or above(download)
Let us start with our Spring MVC based Freemarker application.
Step 1: Getting Started
Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.
After selecting Dynamic Web Project, press Next.
Write the name of the project. For example Freemarker_SpringMVC_example. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.
Once the project is created, you can see its structure in Project Explorer. This is how the project structure would look like when we finish the tutorial and add all source code.
Till this step, our basic Eclipse web project is ready. We will now add Spring MVC and Freemarker support to this project.
Step 2: Adding Spring MVC Support
First copy all required Spring MVC and supporting JAR files in WebContent > WEB-INF > lib folder. Create this folder if it does not exists. Don’t worry if you do not have these JARs. You can download the complete source code with JAR files at the end of this tutorial.
Next we change web.xml (deployment descriptor) and add Spring MVC support to it. If you do not know why we do this, I strongly recommends you to go through Spring 3 MVC Tutorial series.
Related: Spring 3 MVC hello world example
Update the web.xml with following code:
The above code in web.xml will map
DispatcherServlet with url pattern
*.html. One thing to note here is the name of servlet in
<servlet-name> tag in web.xml. Once the DispatcherServlet is initialized, it will look for a file name
WEB-INF folder of web application. In this example, the framework will look for file called
Create a new file
/WebContent/WEB-INF/ folder. This is the spring configuration. Copy following code into it.
In the above xml configuration file, we have defined a tag
<context:component-scan>. This will allow Spring to load all the components from package
net.viralpatel and all its child packages. This will load our
UserController class. Also we have defined a bean
viewResolver. We uses
org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver as view resolver. Also notice how we defined bean
freemarkerConfig. This defines Freemarker configuration, in our case just one property
templateLoaderPath; the path where we will store all our
.ftl template files.
Thus the bean
viewResolver will resolve the freemarker template view. Note that in our UserController class, we have return a view name “index”. This will be resolved to path
Step 3: Create Spring MVC Controller
The UserController is a simple Spring 3 MVC controller that handles request/response. We have created a
private static List userList which stores the user entries. Note that in ideal example you would like to read data from database. But for sake of simplicity we will stick to static List
There are two methods
add(), both mapped via
@RequestMapping annotation. The index() method will simply store the users list in model object and render “index” view (index.ftl). And the add() method gets the user details from html form and add it inside static List. Once the add() has added user, it simply redirects the request to /index.html which will render the user list.
Apart from the above
UserController class, we will also need a bean class
User which holds the user information like firstname, lastname etc.
Now add Freemarker view in your project.
Step 4: Create Freemarker Template Files
Create a new file
index.ftl under folder
/WebContent/WEB-INF/ftl/. Copy following content into it.
This is a simple FTL template file. We just iterate model[“userList”] list in a loop and prints user’s firstname and lastname in table.
That’s All Folks
You may want to run the application see the result. I assume you have already configured Tomcat in eclipse. All you need to do:
Open Server view from Windows > Show View > Server. Right click in this view and select New > Server and add your server details.
To run the project, right click on Project name from Project Explorer and select Run as > Run on Server (Shortcut: Alt+Shift+X, R)
Download Source Code
Reference : viralpatel.net