I'm writing webapp using Spring 3 MVC with annotations. I use jsp for view layer. I have a few basic templates and many templates that I want to include into this basic templates in depend of controller.
I know that I can add template name to model inside controller, but it is not best way I think. I think some declarative way must exists.
What the best way to do this.
Thanks.
You may try Apache Tiles (integration with Spring MVC, petcare sample).
With Tiles you can break your templates into subtemplates, assemble them declaratively into tiles' definitions and use definition names as view names returned from controllers.
Related
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/
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.
I am supposed to redesign a web application that was originally created in JSP. However, I have two important requirements that this application should meet.
This new web application shall be able to keep its existing direct URL structure, that looks like this: http://existingurl.org/appname/file.jsp?id=0000000000001&optionalParams=something
It shall Support Javascript and AJAX (But I think that this one is not really a problem nowadays)
My first choice was spring MVC due to its popularity, but as far as I know the URL structure in Spring MVC would not support the file.jsp part of the URL. In Spring the URL would look like this:
http://existingurl.org/appname/file/id/0000000000001?optionalParams=something because it tends to hide the extension.
Is there any way to support this direct URL from my first requirement with Spring MVC? Or would you recommend me any other framework?
Spring mvc will absolutely let you keep your mapping. You can map it how you like!
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.
I'm still just getting my feet wet with Spring, but am curious if it would be possible to somehow build a nested hierarchy of views. The goal would be to have a parent view/JSP with the header/footer of the page, and then have descendant views that would be wrapped with the parent.
Is this possible within the context of Spring's MVC architecture? If so, what my first steps be?
I recommend using Spring MVC with Tiles. You can configure a parent 'template' tile that contains your header and footer, then have each tile extend that template. You use the name of the tile as your MVC view rather than using the JSP as a view directly.
An excellent way to see how to use Spring MVC with Tiles is to create a project with Spring Roo. Roo is a developer tool that helps you quickly start new Spring projects. It will create a new web application for you with all the views using Tiles; and those views will be hierarchical like you describe. Even if you don't use Roo to create your final project, you should find it a useful example of how you can use Tiles with Spring MVC.
To roll it yourself, using <jsp:include> you can do this. You might have a controller for an entire page, and the JSP for this page will have one or more jsp includes that refer to other controllers that render further JSPs. These can use jsp includes all the way down.
See JSP-based Templating with Spring
... I opted for a 'home brew' solution only because the project would benefit more from lightness and simplicity of that. If I could be somewhat certain that my project will cross the line where another 'straight jacket' of a presentation framework would be beneficial that would be a different story.