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.
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.
I am looking for some framework in java which can directly create html forms from database tables for crud operations on DB?
Spring Roo builds entities, pages and controllers based on Spring MVC,JPA. It can detect changes in the database. It is interesting that leaves no dependencies at runtime so, elements can be modified as regular Spring MVC elements, or you can add your own controllers, views, etc.
It has its limitations, of course, but it can be useful.
Would this work for you? http://incubator.apache.org/isis/index.html
Telosys Tools (an Eclipse plugin) can generate a full Spring MVC web application
from an existing database.
See the tutorials : https://sites.google.com/site/telosystutorial/
( if necessary the templates are customizable )
Maybe Spring Roo can help you:
http://www.springsource.org/spring-roo
http://static.springsource.org/spring-roo/reference/html/base-dbre.html
I think this is a big missing type framework in java. Roo works quite well, but it generates many files for each entity and seems to hava problems when you want to edit/remove some of them (at least it happened when I was trying).
I would like something like django admin, it should generate the page dynamically for each entity, probably with a view class describing the view.
For customizing a view even further it should follow a convention like:
views/domain/Entity.jsp -> replaces the whole view for the entity
views/domain/Entity_fieldname.jsp -> used as template for the field
Same thing for controllers, in roo there are many class that do almost the same thing. Why not use a default controller, and if the user defines a controller related to a entity, he could override the methods.
Of course this is not detailed enough, but I think pure java can improve a lot in making simple CRUD screens easy.
At least, this is the open source side. Inside business there are frameworks for CRUD, sometimes similara to this I describe. The hard part is making this framework decoupled from the other tools.
NetBeans can generate JSF pages based on your entities to do basic CRUD operations.
How-To: Generating a JavaServer Faces 2.x CRUD Application from a Database
try this:
barahisolutions.com/code-generator.htm
Its a Swing based code generator to reverse engineer databases and generate HTML forms ,entities,daos.
I have created a java based tool called Enfacade, that will generate CRUD screens from your DB tables.
The screens are data grid based and they will allow multiple grids to give one-to-many functionality.
You can then deploy to any java web application server using the runtime engine.
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.
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.