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.
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
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.
Related
I have a J2EE site which is running fine, main functionalities include display contents from MySQL, form validating, etc.
I want to pass this site to Spring MVC framework in one or two weeks, but I know nothing about Spring MVC. I decide to use Spring MVC + Maven.
I'm searching for good learning resources to let me do this. It should be easy to learn and project-based. I've read some of the guides from Spring website, it's good but too slow. I just want learn to build the same site with Spring and that's it.
Thanks.
You can find a lot of documentations, materials and examples by searching on Google and GitHub. You can also check http://spring.io, which contains excellent guides and examples.
Keep in mind Spring is a very large framework...you won't be able to learn everything about it by learning one or two tutorials.
http://courses.caveofprogramming.com/ - you can try send an email and get the rest of his video tutorials. they are very good
https://www.youtube.com/watch?v=mH2jx2RCu7o&list=PLmcxdcWPhFqMq2BctGktOcIJKUw23wJeh
http://docs.spring.io/spring/docs/current/spring-framework-reference/pdf/spring-framework-reference.pdf
If you are looking for paid resources then you should go with
Spring & Hibernate for Beginners (includes Spring Boot) 5*
Spring Boot Microservices with Spring Cloud Beginner to Guru 4*
Learn Spring Boot - Rapid Spring Application Development 4*
Learn Spring: The Certification Class
If you are looking for free resources then
https://www.youtube.com/c/JavaGuides 5*
https://www.youtube.com/c/LearnCodeWithDurgesh 4.5*
https://www.youtube.com/c/Telusko 4*
Tip: The best way to learn Spring Boot if you have prior exposure to Java and Spring Framework is to build a simple application.
I am starting a new pretty big webapp and I am using Spring MVC for complete MVC architecure and I really don't want to change that. On top of that I am looking for a view technology and finally closed with JSF as JSF/facelets is in official EE specification which means they will be the future. Plus prime faces looks promising in acheiving good UI. I know JSF is a MVC framework and I just want to liverage its view part to reduce the development time and at the same time acheiving good and flexible responsive UI
But I went through several posts on StackOverflow itself which prohibits the use of Spring MVC + JSF. E.g, this one
Using JSF as view technology of Spring MVC
This question was answered in 2011 and now new Java EE specification also got released and many changes has happened. Is this point still stands that we should not use JSF+Spring MVC? If it is true, then what are the replacements. One option that looks good for me is Thymeleaf, but only thing that is troubling me is that will it be a good idea to neglect an official specification?
JSF can probably be integrated (I'm not very sure, see the links at the end of my answer) with Spring MVC but it does not look a very good fit to me.
Primarily since JSF is component based, while Spring MVC is action-based. In Spring MVC, the controllers do the processing and can pass the results to anything that renders the view (jsp, html, thymeleaf, apache tiles etc.). Thymeleaf is a good option and I like its approach to templates.
Other alternatives I would suggest to look instead of JSF:
JS frameworks:
I personally prefer this for my projects and there are tons of options to choose like jQuery/jQuery UI, ExtJS etc. for creating you views. You can combine these and make their elements work with fluid frameworks like Bootstrap so that they behave nicely on all screens sizes (not sure how PrimesFaces/RichFaces components behave on all devices)
Frameworks that compile to JS:
Here you have two good frameworks to choose from - Vaadin and GWT. I've used only Vaadin and not GWT so will not comment much on it. Choose this if you don't want to directly fiddle with javascript.
It is not true that only if you use the official Java EE spec you can be in sync with the future. HTML5, JS frameworks etc. are as much the future as JSF is, if not more! Ofcourse JSF of late looks good too (I have not made any production code with it yet, nevertheless have explored it in hobby projects) but you are better off using it on its own with PrimesFaces/RichFaces and not combine it with Spring MVC.
On a middle ground, you can use Facelets instead of Thymeleaf for templating views in Spring MVC. On the other side, you can use JSF with Spring DI, but as I said earlier don't mix JSF and its component libraries with Spring MVC.
Update: Just in case if you absolutely want to try it, here are some links:
http://spring-explored.blogspot.in/2011/11/using-jsf-with-spring-mvc.html
http://papweb.wordpress.com/2011/07/29/spring-mvc-3-jsf-2-with-maven-2-and-tomcat
I'm looking for what view technology would be considered the best approach for Spring MVC 3.0 which is flexible, maintainable and allows multiple rendering technologies such as HTML, PDF etc...
We're looking to develop our next web application using Spring MVC 3.0 and have settled on Hibernate for persistence but are still trying to decide which is the most appropriate view technology.
We will require support for PDF reports but would most likely write these using iText.
Are there any maven archtypes which would setup a recommended application complete with persistence and view technology?
The spring samples use a bunch of different technologies from JSTL to Apache Tiles, we've only ever used JSP in the past since we moved to .NET in 2003 and are moving back to Java now.
There's really nothing wrong with using JSP, if you use the most recent version. You can certainly choose Freemarker, Velocity and other cool viewer, but if your entire team is already comfortable dealing with JSP, you need to take account of your project deadline and all the learning curves when taking on a new technology. If it makes sense, sure, it doesn't hurt to learn another new thing.
In my case, I use JSP with Sitemesh as my template. I used JSP/Strut Tiles combo for quite awhile but I find the template to be too intrusive, Sitemesh seems to be more seamless to me because it doesn't clutter my actual JSP files with some Tile specific custom tags.
Below are a few.
Freemarker
Apache velocity
JSP (the new 2.1 alright with improvements like functions, simplified custom tags, etc.) with apache tiles
Here is a doc that talks about integration with view technologies
We are currently using Jsp 2.1 with spring webmvc 3.0x with apache tiles on google appengine and it servers our purpose quite well.
How about Apache Velocity
That's the most popular template engine I know of. Would like to get more suggestions though.
Update:
Freemarker is another
Source
If I were you, I would stick with jsps for now. You can mix and match your views in Spring if you decide certain flows will require something else. For example, you will need to use different view types if you plan to use any ajax style processing.
JSP : easy to use and maintain.
Other are Apache Velocity, Freemarker.
I personally would recommend JSP
I want to develop a website with java but I'm absolute beginner in java web development.
I want to use a framework that uses the MVC pattern and Ajax.
I did some search and found that Spring or Struts are suitable but I'm not sure.
could you please recommend a framework?
Play Framework might be a good option because of its incredible simplicity.
I'd recommend Spring:
Developing a Spring Framework MVC application step-by-step
The Stripes Framework is also worth consideration and can be used along with Spring.
http://www.stripesframework.org
It's easy to use and easy to configure. Unlike Struts, which is fairly old hat these days.
There are a plethora of frameworks now and it's worth checking each one that will suit your needs. It's a personal thing and it's good that we're not all restricted to a few.
JRapid is very easy to use. You'll get a working application in minutes and it generates AJAX powered user interface.
I work with Spring Webflow - technology based on Spring MVC. Webflow is described in official help pretty well. Personally, it think Webflow is much more usable than bare Spring MVC, however there are always someone who disagrees.
Moreover Spring (not talking about MVC specifically) can be used outside of web projects, so it seems to me it is advantage of Spring.
Cannot say anything about Struts.
I would recommend GRAILS for fast rapid web application development, that includes scaffolding functionality and web page generation based on data models.
https://grails.org/learn
It is the fastest way for MVC developers.
I am embarking on a new RIA project with Java on the backend. I'm the only developer, and the app is a line-of-business application. My current stack looks like this:
MySQL || Spring(JdbcTemplate for data access) || BlazeDS (remoting) || Flex(Cairngorm)
My question is: what changes can I make to improve productivity? Manually coding SQL, server-side entity objects, client-side value objects and all the Cairngorm stuff is obviously a drag, but I'm not sure what higher-level frameworks to introduce.
What Flex/Java stack has served you well?
Manually coding SQL
Hibernate is an option to cut this out.
One thing that may be of interest is Grails with the available Flex Plugin. It's built on Spring, Hibernate and BlazeDS, so it's all there for you. It was unbelieveably easy to get it remoting stored objects and responding to AMF calls. I was using this and then moved over to a RESTful E4X implementation as I found it a lot easier to debug and tweak as I could inspect the server output in a browser and have tighter control over exactly what I returned to my Flex app (lazy loading problems in collections using BlazeDS were a headache at times).
Here is a quick tutorial showing how easy the whole Grails + Flex Plugin stack is: BlazeDS in Grails
I would seriously reconsider using Cairngorm. In my opinion it's a pretty bloated framework that introduces a lot of abstraction you'll never use. Check out:
http://code.google.com/p/swizframework
http://www.spicefactory.org
Both introduce the concept of dependency-injection into your Flex app.
Also +1 for Hibernate. I would use the standard JPA annotations on a Java class and extend that with Hibernate Annotations as you find you need additional functionality.
Check out springsource.org's new Spring BlazeDS Integration Project
Spring BlazeDS Integration is a new
addition to the Spring portfolio, and
a component of the complete Spring Web
stack. This project's purpose is to
make it easier to build Spring-powered
Rich Internet Applications using Adobe
Flex as the front-end client. It aims
to achieve this purpose by providing
first-class support for using the open
source Adobe BlazeDS project and its
powerful remoting and messaging
facilities in combination with the
familiar Spring programming model.
As alternative to hand-coding sql, aside from hibernate, you might wanna consider JPA/Toplink. And since you are already from a Spring camp, check out Spring ActionScript (formerly known as Prana), it's an IOC framework for Flex. It solves many inherent problems in Cairngorm. Also a good IDE that supports Flex/Actionscript like IntelliJ IDEA 8.
Whats missing between Flex and BlazeDS is, it doesnt have any sort of landing page where you have a listing of all the available services by just typing a url on the browser (similar to webservice endpoint).
You can try GraniteDS. It's an alternative to BlazeDS, with an actionScript generator, Spring integration (an Spring security), and if you use an JPA implementation, help you with the Lazy Initialization (in a transparent way).