James
James

Reputation: 273

Spring MVC - Layouts

I'm in the process of learning Spring, having come from working in a PHP/Zend environment. I'm trying to work out how to do all of the common things that I usually do in a project, from AJAX requests to form handling etc. It all seems pretty straight forward but I'm having trouble with one particular area at the moment, and that's layouts

I've looked at Velocity, SiteMesh and Tiles which all seem overkill, and also fail to address another problem (afaik) relating to layouts which is the injection of "widgets" for lack of a better description.

What I'd like to be able to do is to easily say "This controller (or method) should use this layout". I don't need anything more complicated than that. I found this blog post which with a little adjustment would give me what I need I believe: http://blog.tuxychandru.com/2009/12/simple-layouts-with-jsp-in-spring-mvc.html

On to "widgets". Lets say I have my "layout.jsp" which includes my top header, which in turn includes the currently logged in user, a dynamic nav, CSS/JS urls which need to be dynamically prefixed. In other words, the layout needs to be able to either grab the data it needs, or have it injected. As far as I can see I have two options. First option is a custom taglib. Second option is to make a Layout bean which can then be injected with a set of other beans (as defined in the config) which could in turn be injected in to the model for the JSP to use.

Does anyone have an opinion on the preferred approach out of the two above? The taglib seems simpler, though less elegant.

Are there any layout/templating engines out there that integrate with Spring and provide the ability to pass through arbitrary beans like this?

UPDATE: I notice that Tiles has the concept of a ViewPreparer which would resolve the problem around injection of objects not related to the controller.

Thanks in advance,

James.

Upvotes: 1

Views: 1689

Answers (1)

pap
pap

Reputation: 27614

Tiles is actually pretty easy so I wouldn't discard it as "overkill". I use it quite often. As you have already discovered, with the ViewPreparer you can also emulate a component-like behavior pretty closely.

Another way to go is to use JSF for rendering. I wrote up a bootstrap for Spring MVC using JSF 2 a while back.

Upvotes: 1

Related Questions