Charles Forsythe's Resume Charles Forsythe's Resume

PDF

Download my Resume(PDF)

My Technical Blog My Technical Blog

« Back

Spring MVC Portlet + Freemarker

Search around for any "Spring MVC Porlet" tutorial or draft and you will undoubtly find the phrase "Spring Portlet MVC Framework provides easy integration with popular view-rendering technologies such as JSTL, Apache Tiles, Apache Velocity, and FreeMarker."

The way that Freemarker does this is to instantiate a ViewRenderServlet.  This Servlet can be invoked from the Portlet to use Servlet-oriented view renderers to render the Portlet.  It's a nice hack, and your can find dozens of examples where it is demonstrated with JSTL.  This is a problem because if you modify the example to use Freemarker, it doesn't work.  It doesn't work because of the way that the FreemarkerView class relies on auto-wiring to configure it.  What you will get in the logs is this:

	java.lang.NullPointerException
	        at org.spring(...)FreeMarkerView.getTemplate(FreeMarkerView.java:350)
	        at org.spring(...)FreeMarkerView.checkResource(FreeMarkerView.java:203)
	        at org.spring(...)UrlBasedViewResolver.loadView(UrlBasedViewResolver.java:421)

I found a 2-year-old bug report about this claiming that the Freemarker renderer simply wouldn't work with Portlets and that maybe they'd fix that in Spring 3.1.  It turns out that the system works fine if you initialize it correctly.  In order to do so, you must include the following in your web.xml:

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

I had already figured this out myself, but was still searching for a fix because my FTL referenced "hellowWorldMesssage" when it was being passed "helloWorldMessage" (look carefully to see the difference).  Now that I'm set up with my favorite rendering pipeline, I'm wondering how many more hacks I'll need to do to get Freemarker to work well in a Portlet environment.

Comments
Trackback URL:

Emmanuel
I have tried your approach but I am still getting the NullPointerException:

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Basics-portlet</­display-name>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-­class>
</listener>

<servlet>
<servlet-name>view-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-clas­s>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>view-servlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>

<jsp-config­>
<taglib>
<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
­<taglib-location>
/WEB-INF/tld/liferay-portlet.tld
</taglib-locati­on>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/aui</taglib-u­ri>
<taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
</taglib>
</j­sp-config>

Any help will be very much appreciated.
Thanks
Posted on 12/31/12 8:39 AM.
I've just discovered that LifeRay's blog applet doesn't automatically subscribe me to comments, so I'm answering this months after it was posted.

Emmanuel doesn't tell me what LifeRay stack he's using, but I'm guessing LifeRay 6.1.1 on Tomcat. Why am I guessing that? Because it broke my fix. Here's an new fix:

http://charles.forsythe.name/home/-/blogs/liferay-6-1-1-on-tomcat-breaks-spr­ing-and-freemarker-quick-fix-edition
Posted on 3/13/13 5:19 AM in reply to Emmanuel.

Hello World Hello World

Welcome to Liferay Portal Community Edition 6.1.0 CE (Paton / Build 6100 / January 6, 2012).

Publications Publications

Instant FreeMarker Starter

Instant Freemarker StarterThe Instance Freemarker Starter offers a quick introduction to using Freemarker in Enterprise projects. All of the key features of Freemarker Template Language (FTL) are covered with realistic examples. Also covered are integration with MVC frameworks such as Spring.

 

The Know-it-All Guide to java.langString

This guide provides a detailed description of how to work with Java's most-common class.  This included issues affecting memory usage, internationalization (i18n) and character encoding.

Second Edition: Now updated for Java 8!

(This has been taken offline for further edits)