Wednesday, April 25, 2012

JSP tricks to make templating easier?


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: http://stackoverflow.com/questions/490390/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.


Source: Tips4all

No comments:

Post a Comment