18
votes

Is it possible to use Twirl with Spring MVC as the view technology? I would like to use something in place of JSP, and Apache Tiles feels cumbersome to me. Twirl the view technology for the Play Framework looks very cool, and similar to ASP.NET Razor.

https://www.playframework.com/documentation/2.3.x/JavaTemplates

I did a quick Google search but did not uncover much.

2
You may try using JSF 2.x for view technology as shown in several blog entries and q/as here. I haven't worked with Twirl but seems that may fit for what you want/need.Luiggi Mendoza
Can you use JSF as a view technology with Spring MVC. I thought you could use Spring + JSF but it didn't make sense or work out well with Spring + Spring MVC + JSF because they are both MVC Frameworks. I may have misunderstood what I've read though so please feel free to correct megreyfox
@greyfox would this help? github.com/67726e/Spring-MVC-TwirlAndrei Sfat

2 Answers

0
votes

I didn't used Play Framework so I'm not familiar with it's architecture. But I found your question interesting and did some research.


Twirl Template Engine with Spring MVC Demo App

But I did found some useful resources that might help your case.

Spring MVC Twirl GitHub

This repository has an identical example of what you're looking for.

To be honest, I haven't tried to run this code myself but it seems like a trustworthy user.


Also, I did found a Blog Example which explains how to run Twirl Template Engine standalone without Play Framework. This doesn't cover your use-case but it suggests it's possible to use Twirl Template Engine without Play Framework (hence with right configs it's possible to use it with Spring)

Standalone Twirl Template Engine Usage Example - Blog and GitHub Repo

Standalone Twirl Template Engine Usage

Standalone Twirl Template Engine Usage GitHub Repository


Different Template Engines for Spring - Does not mention Twril

Also, if you wan't a great reading reference I always visit Baeldung website

I found a blog post about different template engines for Spring. They don't cover your use case, but based on this article it is possible to use variety of different engines given that you have proper dependency (e.g. maven, gradle) and do a proper configuration.

Does not mention Twirl, but it might give you some idea how to do it

Template Engine for Spring Blog Post

-3
votes

If you are looking for a templating framework to use with Spring MVC I recommend using Spring's Thymeleaf.

This answer also provides some valuable feedback relating to the issue: HTML templates in spring boot similar to play framework

Spring framework does not force you to use any particular view technology. You can use anything that can be integrated. The topic is covered in the documentation (https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view) where you can read about the details on popular choices. However, the most popular is Thymeleaf, which used to be a part of Spring MVC before separation into an independent project. You can find a clear example on how to use it with Spring Boot in the getting started guide.

The templates in Play framework created using Twirl framework are converted into Scala code. You simply pass the data model via template parameter list, just like you do with any method.

In Spring framework you put all date that you want to expose to your view in a so-called model map, which is a simple key-value storage. In the view technology you refer the values using corresponding keys.