At work I’ve been tasked with turning a bunch of HTML files into a simple JSP project. It’s really all static, no serverside logic to program. I should mention I’m completely new to Java. JSP files seem to make it easy to work with common includes and variables, much like PHP, but I’d like to know a simple way to get something like template inheritance (Django style) or at least be able to have a base.jsp file containing the header and the footer, so I can insert content later.
Ben Lings seems to offer some hope in his answer here:
JSP template inheritance
Can someone explain how to achieve this?
Given that I don’t have much time I think dynamic routing is a little much, so I’m happy to just to have URLs map directly onto .jsp files, but I’m open to suggestion.
Thanks.
edit: I don’t want to use any external libraries, because it would increase the learning curve for myself and others who work on the project, and the company I work for has been contracted to do this.
Another edit: I’m not sure if JSP tags will be useful because my content doesn’t really have any template variables. What I need is a way to be able to do this:
base.html:
<html><body>
{ content.body }
</body></html>
somepage.html
<wrapper:base.html>
<h1>Welcome</h1>
</wrapper>
with the output being:
<html><body>
<h1>Welcome</h1>
</body></html>
I think this would give me enough versatility to do everything I need. It could be achieved with includes but then I would need a top and a bottom include for each wrapper, which is kind of messy.
As skaffman suggested, JSP 2.0 Tag Files are the bee’s knees.
Let’s take your simple example.
Put the following in
WEB-INF/tags/wrapper.tagNow in your
example.jsppage:That does exactly what you think it does.
So, lets expand upon that to something a bit more general.
WEB-INF/tags/genericpage.tagTo use this:
What does that buy you? A lot really, but it gets even better…
WEB-INF/tags/userpage.tagTo use this:
(assume we have a user variable in the request)
But it turns you like to use that user detail block in other places. So, we’ll refactor it.
WEB-INF/tags/userdetail.tagNow the previous example becomes:
The beauty of JSP Tag files is that it lets you basically tag generic markup and then refactor it to your heart’s content.
JSP Tag Fileshave pretty much usurped things likeTilesetc., at least for me. I find them much easier to use as the only structure is what you give it, nothing preconceived. Plus you can use JSP tag files for other things (like the user detail fragment above).Here’s an example that is similar to DisplayTag that I’ve done, but this is all done with Tag Files (and the
Stripesframework, that’s the s: tags..). This results in a table of rows, alternating colors, page navigation, etc:Of course the tags work with the
JSTL tags(likec:if, etc.). The only thing you can’t do within the body of a tag file tag is add Java scriptlet code, but this isn’t as much of a limitation as you might think. If I need scriptlet stuff, I just put the logic in to a tag and drop the tag in. Easy.So, tag files can be pretty much whatever you want them to be. At the most basic level, it’s simple cut and paste refactoring. Grab a chunk of layout, cut it out, do some simple parameterization, and replace it with a tag invocation.
At a higher level, you can do sophisticated things like this table tag I have here.