Liferay : Create a Spring MVC Portlet + Thymeleaf

Why ?

I work on various Liferay projects, we often use Liferay/Spring Portlet MVC + JSP technologies. I’m tired of using JSP, I won’t explain why, there are a lot of its drawbacks described on the Internet.

I know that it’s possible to make “almost good” JSPs without any Java scriptlet, but it’s boring and the final JSP content is kind of a mess. Taglib system is too heavy and too boring to maintain. The only thing I like in JSP is EL (Expression Language) syntax.

So what’s next ? I just want to use a simple templating system with a nice syntax and without the ability to write pure Java. I want a clear separation between business logic and view logic, I want the V of MVC.

Edouard, a friend of mine, started his own business a few months ago. We often have discussions about tech stuff, for the view part of his software he used Thymeleaf.

Thymeleaf templates are 100% valid HTML, you can ask a designer to build the UI he wants, then you would have to add some technical information to have the final rendering. The syntax is nice (EL syntax is valid), it’s easy to integrate with Spring, the documentation is pretty clear. This seems to be a good candidate.

So what’s next ?

After all the discussions & searchings, I decided to try to bootstrap a basic Spring + Thymeleaf Liferay portlet.

There are few tutorials about Spring+Thymeleaf on Thymeleaf’s documentation. To be sure that Thymeleaf can be used in Liferay, I created a simple portlet that cover these portlet phases : Action, Render and Resource. I hosted the code on my Github account.

Common questions

Liferay heavily uses taglibs, how can I use them in Thymeleaf?

You can’t use them anymore, taglibs are for JSP.
After all, taglibs are based on java so it’s possible to migrate the important taglibs (like the ones that creates render/action/resource URLs).
Thymeleaf extending system is pretty cool, there is some time to spend to get back the important taglibs but it’s doable.

People are used to JSP syntax, how can I tell them to use Thymeleaf ?

This is the first time that I’m using Thymeleaf, as of now I can say that it’s far easier to read compared to JSP. You can reuse EL syntax in those templates thus developers won’t get totally lost 🙂

Conclusion

This first attempt is pretty basic but it is working fine, there are a lot of pending topics to work on so that Thymeleaf could be fully usable in a real Liferay use case.

Soon I’ll have a real portlet to work on, I’ll try to push the use of Thymeleaf. Then, I’ll see if it has been a good idea or not.

I’ll keep you updated!

Useful links

http://docs.spring.io/autorepo/docs/spring/4.2.5.RELEASE/spring-framework-reference/html/portlet.html
http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html

3 thoughts on “Liferay : Create a Spring MVC Portlet + Thymeleaf

  1. David says:

    Hi Vianney,

    I’ve also decided to bring together spring mvc with thymeleaf for liferay 6.2 and i’m facing to an issue, i wanted to know if you had the same :

    Deploying the portlet has you described in your github, it happens sometimes where themeDisplay variable in thymeleaf page is null. When i use debugging, i saw debugging going through other @ModelAttribute method and not inside the @ModelAttribute(“themeDisplay”). It often happens when i start again my server.

    Sounds like DispatcherPortlet is not mapping correctly in render phase.

    Did you got any trouble like this ?

    Thanks in advance.

    (I’m using liferay 6.2, spring 4.1.5-Release)

    Best regards,
    David.

    • Hey David,

      It’s working fine for me, as ThemeDisplay is set in the request from ServicePreAction, it has to be available each time you go through your controller.

      Could you share some bits of your code via email ?

      Regards,

      Vianney

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s