Seems like JSPs, Velocity, Freemarker etc. can offer so-called “inner templating”: I can describe an outer template, then define inner parts. Like this (simplified):
main.jsp:
<html>
<body>
<div class="container">
<%@ include file="menu.jsp"%>
<%@ include file="content.jsp"%>
</div>
</body>
</html>
So I can define menu.jsp and content.jsp and all works just fine. But here outer block has references to inners. Not very suitable for me.
I’m looking for technology for Java, that can let me implement something like this:
some_block.jsp:
<template file="main_template.jsp">
<div>
... my content here
</div>
</template>
main_template.jsp:
<html>
<body>
<div class="container">
<inner_content />
</div>
</body>
</html>
I. e. vice versa – inner blocks have references to outer. Is it possible with JSPs? If not – what should I use with Spring MVC?
EDIT: Why it be more comfortable for me: when Controller receives a request, it detects what view it should render. So I can render, for example, feedback form:
feedback.jsp:
<template file="main_template.jsp">
<form> ... feedback form content here ... </form>
</template>
or a product page product.jsp:
<template file="main_template.jsp">
<div> ... product page content here ... </div>
</template>
and there is no need to describe page structure for every kind of pages, and there is no need to pass any parameters to outer template to render content correctly. And even no need in dynamic compilation – all pages are just an implicit set of precompiled servlets.
I created a JSP taglib you might be interested in:
http://www.inamik.com/projects/webframes/
Here is how I would implement your request using the WebFrames taglib
Notice that this is an exact 1-to-1 correlation to your example, but unlike your example, WebFrames allows you to create unlimited place-holders so you could have things like dynamic titles, css-includes, right-channel content, left-navs, etc.
feedback.jsp
product.jsp
main_template.jsp
Official WebFrames Example
Below is the full example set from the website showing how you can create/populate multiple place-holders:
http://www.inamik.com/projects/webframes/examples/simpleexample.jsp
Start by looking at the source for the example main page:
main http://www.inamik.com/projects/webframes/examples/simpleexample.jsp.txt
See how this defines the content sections without defining the layout.
If you look at the layout (‘frame.jsp’) You’ll see it doesn’t know what content is going to be displayed, it just creates place-holders for the content:
frame http://www.inamik.com/projects/webframes/examples/frame.jsp.txt
header http://www.inamik.com/projects/webframes/examples/headersection.html.txt
footer http://www.inamik.com/projects/webframes/examples/footersection.html.txt
This pattern provides a much more flexible solution that creates components that are re-usable and easier to maintain.
NOTES
This was based on earlier work by David Geary:
http://www.javaworld.com/javaworld/jw-12-2001/jw-1228-jsptemplate.html
Also, if you’re willing (or considering) non-JSP solutions, I wrote a Java-based template engine in the spirit of PHP’s Smarty template engine
https://github.com/iNamik/iNamik-Template-Engine
I also have a ‘Frames’ taglib for this engine, which isn’t on GitHub yet.