views:

29

answers:

1

It's common to have many pages in a website that request the same model attributes, for example, a header that renders the current users name, or a column that needs a few server based pieces of information.

...
<body>
<mytaglib:header/>
...
<jsp:include page="footer.jsp"/>
...
</body>

The simplest solution is that every controller method you write that returns views that use the header, or footer also add all of attributes needed by components contained within it, but as the site grows it can become difficult to handle, particularly when you start dealing with pages that don't all have exactly the same components.

Is there a way to encapsulate the logic that adds to the model based on sub-pages or tags?

A: 

Well, you could do that in a few ways. The following pop in mind:

1 - put your logic in a filter that places the info in request scope

2 - Spring has something similar to a servlet filter called an interceptor. You can place your logic here and again save it in request scope.

3 - have the logic behind a custom tag that is inserted into your pages. There is a disadvantage to this because you must insert the tag in each JSP. If you are using a decorator filter like SiteMesh then you place this once in your decorator JSPs and you are done with it.

4 - have the logic in a parent controller that all your controllers extend.

dpb