Monday 25 November 2013

Presentation Layer: Layout

OK, I admit it I have one jar in my lib directory that is from a framework. It's called Stripes. I don't use it for a web framework though. Not that it's not a good one, it is, it's great. It's a big improvement on Struts which, like many of its ilk, it was a reaction against. But just because, you know, I'm trying to do this No Framework web app thing. Anyway let me start at the beginning...

The topic for this post is how do you have a reusable element of your web app if you're not using a framework? How do you lay out your pages without having the same code over and over?



Great question, the answer is... you can't. The straight forward J2EE app with servlets and JSPs doesn't offer any support for reusable components or layout templates, or anything.

So here I'm going to recommend Stripes, just because it has a fantastically simple layout engine.

The idea is that you have one or more layout jsps, which define the location and optionally the content of components, along with any HTML that you want to appear on every page. Now, in your normal jsps, you may implement a layout by wrapping any jsp code in a layout-render tag, which means you can override the content of any of the specified components, if you don't override then the default content will be used.

I won't do a whole blog post explaining how to use it, I'll just send you here:

http://www.stripesframework.org/display/stripes/Layout+Reuse

Read it, use it. You can just include the jar and start using the Layout tags, without making any changes to your servlet code or having to add anything to web.xml or other config.

No comments:

Post a Comment