I'm learning Spring MVC and want to create a site. The main problem is a template system. Should I use JSP / JSF / Apache FreeMarker / Thymeleaf or something else?
I saw a lot of discussion on this subject, but they are all outdated. So, I'm curious, what is fine now?
The best practices for server-side rendering have shifted towards using a template engine. These get the responsibility to merge the data with the template into the actual output.
Current preferences appear to be:
Thymeleaf
FreeMarker
JSP's and JSF are entirely different things and have become out of fashion.
The big plus for using an actual template engine is that you are forced to separate the concerns of gathering the data to present and rendering it; this separation allows you to (unit) test the templates.
Note, however, that the industry is shifting once more towards client-side rendering, where the server just returns the data as JSON-objects and the web application uses some framework like Angular, React, jQuery or Ember to build the pages.
Note on the edit: Originally the list included Velocity, but that is no longer supported by Spring.
You can use any of them as they are supported. JSP, FreeMaker and Thymeleaf are similar by idea: you create a template to be rendered. JSP and FreeMaker lacks some features that are available in Thymeleaf.
I like Thymeleaf's idea where you can load your template to the browser and see how the page is going to be rendered (real). Thymeleaf template is fully featured HTML page. That's not possible in JSP where you have JSP tags and FreeMaker where you have placeholders.
JSF is component based so that's a different approach.
If I have to choose I would use Thymeleaf.
There are many template engines available out there. But Spring Boot officially supports Thymeleaf, FreeMarker, Mustache and Groovy Templates. My preference is Thymeleaf because of its extendability.
There is a detailed comparison on various aspects of template engines explained in the below post.
https://springhow.com/spring-boot-template-engines-comparison/
Related
I am new to Spring framework. I have built simple web application from JSP. I have used by making template in HTML using CSS and Javascript. Now i want to make same project in Spring so Can i implement my template in spring framework or not? If it is possible how to implement it.
Spring MVC can use a variety of view technologies, such as Freemarker, Velocity, Thymeleaf, and Plain Old JSPs.
Your question is not entirely clear, but I think you are referring to having one Template file, with multiple views that use that Template. Is that correct?
To do so there are a few methods.
One is to include a header/footer on each view using jsp:include. See JSP tricks to make templating easier?
Another option is to use Apache Tiles, which uses the Composite View Pattern.
Another option is to use SiteMesh, which uses the Decorator Pattern.
The first option is probably the easiest for a simple site.
Are there any sample / tutorial on Spring MVC with JSP without using Tag Libraries. Pure JSP.
I am not conversant with Tag Libraries and I am good with JSP. So I would like to see if there are any examples and tutorials using pure JSP without ANY tag libraries.
I don't mean to say this in a degrading manner, but if you are good with JSP, you should be able to pick up the Spring MVC tags easily. JSP, custom tags and ELs go hand-in-hand. They are created for a reason: to make your life simpler. A quick example, if you use Spring MVC's form tag, Spring MVC will automatically prefill the form fields for you based on the data you have in the model. Think about how tedious your code will be to prefill the checkboxes/radiobuttons or preselect the drop down lists.
If you are also Spring Security, the provided custom tags allow you to easily control what data to be displayed based on the user roles.
Writing all of that using pure JSP don't even make sense to me... not to mention the amount of time wasted writing less than perfect home grown solutions.
So I understand that JSPs are a mixture of a client-side code (DHTML, JS, CSS, etc.) and Java. In this way, JSPs are kind of like pure PHP or pure ASP. When the web container receives a request for a JSP, it compiles the Java inside the JSP, executes it and then returns the resultant client-side code in the HTTP Response.
I also understand that an alternative to JSPs are Servlets, or a Servlet/templating combo, like FreeMarker. The servlets contain pure Java (business logic), and the templates contain presentation logic.
What I don't understand is how JSF-derived technologies like RichFaces, PrimeFaces and ICEFaces turn Java code into client-side code that can run in a browser. I've also heard that the major draw to these frameworks are the "rich" UI controls they come with, but am having a tough time connecting all the dots.
Do these frameworks get compiled to JS like the GWT does? If that's the case then I would assume that these rich UI controls would be the same as, say, jQuery UI controls that are just pure JS.
If that's not the case then I just don't understand (at all) how these frameworks turn server-side code into "stuff" that can execute client-side.
Thanks in advance!
What I don't understand is how JSF-derived technologies like RichFaces, PrimeFaces and ICEFaces turn Java code into client-side code that can run in a browser
The HTML is generated by encodeXxx() methods of UIComponent and/or Renderer implementations. Those methods are invoked during render response phase, starting with the UIViewRoot which delegates the call all the way through the entire component tree hierarchy.
JSF implementations such as Mojarra and practically all component libraries are open source. To take the JSF standard <h:inputText> component as an example, start looking at com.sun.faces.renderkit.html_basic.TextRenderer class to see all the HTML generating code.
Do these frameworks get compiled to JS like the GWT does?
Absolutely not. Open a JSF page in browser, rightclick and View Source. It's all just HTML code, if necessary along with auto-included CSS/JS files. With GWT it's one and all JS code.
See also:
Java EE 6 tutorial - Creating custom components - to learn how to program it yourself.
What is the difference between JSF, Servlet and JSP?
What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?
No, JSF is not something like GWT. The idea is different. But let's start from the beginning.
Servlets are not really alternative to JSP. JSP is a technology, which is responsible for View part in Model-View-Controller design pattern. Servlets usually play role of the Controller.
In a typical scenario Servlet receives request from the user, parses it and calls bussines logic (Model) - it could be just the set of java classes or EJB components or whatever else which calculates something, talks to database, etc. Then Servlet gets the data from bussines logic (if any) and redirects user to a proper View element - JSP, pure HTML page, some template engine, etc.
JSF is sort of a layer over JSP + Servlets that makes programmers' live easier (although many would strongly diagree with that :) )
JSF page is basically a JSP page, that uses set of specialized tags - components (JSF native tags or RichFaces tags or ICE Faces tags). These tags can be written in Java, can boundle JavaScript, CSS. The final effect is that these tags generate HTML representing "component", for instance fancy table with sorting capabilites.
So you have for instance component that you place on JSP page and it represents nicely looking table.
In addition JSF is armed with standard controller, which is not written in Java, but is a set of navigation rules between pages written in XML.
One more element are JSF Beans, which are bounded to JSF pages and are automatically filled with data taken from these pages. It is often quite straighforward - user fills the form, which is a part of JSF page (form also use specialized tags - components). Once this form is submitted the JSF Bean class is filled with data and action method is called. The action method returns a string, which is used to identify page to which user should be redirected (the rule is taken from XML controller configuration).
So in case JSF the frontend part is generated in backend - JSF tags (or components, as people call them) are interpreted in the backend and the backend generates HTML from them.
This could be contrasted with thechnologies, which separates frontend and backend. Backend receives and returns data in the form of XML or, more often, JSON and exchanges them with frontend part of the application. Frontend could be written in JavaScript, Flex, etc. So backend and frontend are totally separated.
What's better?
We are going to start a new web project and it's a question which technology to choose.
Project includes Spring MVC + WebFlow.
Any good/bad experience in support, extending, performance?
Thank you.
Velocity and Tiles are two different beasts — Tiles is a layout engine and Velocity is a template engine. They do not intersect anywhere in terms of functionality.
From your question I can guess you probably won't be using JSP. That's a smart move. Velocity is one of the template engines out there and it does an absolutely splendid job.
And if you choose to follow the template engine route for your view, then check out commentit. It's a small, simple and fast layout engine I created. It might serve your purpose perfectly.
How do you want compare Tiles with Velocity? - Tiles is a way to compose page fragments, while Veleocity is a more complete template engine, better comparable with JSP than Tiles.
Anyway: I used Spring MVC with Tiles and JSP: It worked greatly, saved a lot of time (toward just using JSP, or JSP with Sitemash), and I did not noticed any performance problems. (But the web application was never used under high load.)
Use Tiles if you have some seperate HTML files that you want to bring together in a Template (i.e. you have a seperate page for a Header, Footer, Sidebar and you want to bring them together and display them in a sort of Newspaper-like format).
Use Velocity if you want to bring dynamic content across from a Java backend and inject those values into a full HTML page (i.e. you have a HTML table to display a selection of Cars, and all of your cars are stored in a Database. Using JPA, you could get that Car data out of the database, and into a List<Car> maintained in an EJB Bean, Spring #Component, or similar. Then, using Velocity, you can store that information as the bucket item inside a Map, and use the VTL mark-up to refer to the key value items in the Map so that they can be rendered as part of the HTML response.
To achieve the above, Velocity positions itself as an outright Front Controller Servlet, or is wrapped by another MVC framework (i.e. Spring MVC provides a View Resolver which wraps the functionality provided by a Velocity Servlet).
HTML requests are directed to the Velocity Servlet or alternative MVC Framework Servlet via web.xml configuration. As part of the servlet response, your pre-baked HTML view, complete with Velocity VTL mark-up, is enriched with Map data.
Effectively, and in summary:
Velocity competes in the same space as JSP.
Tiles is more akin to page transclusion. In the PHP space, Smarty is a popular cousin.
You don't choose between them, but very well could use both.
There is a Velocity plugin for Tiles 2.2 so you can use both - with velocity you will access context and build your bricks dynamically like with JSP and tiles will combine your website bricks together. However Tiles is not allowing to do many thigns (at least I haven't discovered them yet) and its documentation is very old and bad compared to for example Spring or JSF one. So you can consider using different technology instead.
I have Tiles references in my currect project because Roo did it for me but right now I'm moving everything to JSF.
Use both. Tiles and Velocity integrate very well and solve different problems. You can do some Tiles-ish stuff with Velocity's #include and #parse directives, but Tiles does that composition stuff better.
If I let anyone modify a freemarker viewpage, can I somehow make it hack free?
I know I read somewhere that I can make disable scriplets, but that was for .jsp pages so not sure if it will work with freemarker.
I basically want a way where I can set the attributes that will be available on the page, and let web designers go into the page and edit it all they want (all the while making it secure).
i.e. I don't want people to be able to access any of objects in the request pipeline, or output my connection string to the page etc.
Request and other objects are exposed to the FreeMarker template by Spring's FreeMarkerView, when FreeMarker is used as a Spring MVC view technology.
To have a full control over the data being exposed to the templates, you can use FreeMarker directly, as described in the FreeMarker docs. However, you can still use Spring's support for FreeMarker configuration (FreeMarkerConfigurationFactoryBean).