12

I have a lot of common areas in my web application design, for example footer, header sidebar, blocks...etc and going through all of the application JSP files to change something for example in the header is pretty hectic so I was wondering if I can make something like template files for common areas for example header template, sidebar template, footer template, then I can include such templates in any JSP inside my application?

I am using Spring MVC framework to build my application

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
MChan
  • 6,842
  • 27
  • 83
  • 132
  • 1
    Take a look at the Apache Tiles: http://tiles.apache.org/framework/index.html – Jk1 Oct 13 '13 at 16:08
  • @Jk1 thanks a lot, looks great but I was looking for something rather simpler to implement. In other words how to apply it directly in the application without any additional framework, do you think this is possible? – MChan Oct 13 '13 at 16:11
  • There is a simpler approach around, indeed. Encapsulate header/footer content in custom JSP tag. No additional frameworks/libraries are necessary. Here's the manual: http://docs.oracle.com/javaee/5/tutorial/doc/bnama.html – Jk1 Oct 13 '13 at 16:17
  • @Jk1 one last question if you don't mind please, assuming I want to go advanced in templates and that my application can support multiple themes which can be easily changed by the app admin, how I can do that? Will Apache Tile help me in this task? Is there any other better framework for such task? Thanks for your time – MChan Oct 13 '13 at 22:44
  • Spring MVC has custom themes support built-in, so just use it: http://www.javabeat.net/2013/05/spring-themes. – Jk1 Oct 14 '13 at 05:32
  • @Jk1 You are the first one who proposed all solutions, so can you post them as an answer so that I can select your answer? – MChan Oct 14 '13 at 09:46
  • sure, please find the answer below – Jk1 Oct 14 '13 at 09:58

4 Answers4

11

As for templating you have the following options:

  1. Custom JSP tags. These tags may exist as Java classes or JSP snippets and provide a basic facility to encapsulate and reuse parts of JSP pages (headers, footers, etc.). See more in this manual

  2. Template engines like Tiles or decorators like Sitemesh. They are quite powerfull in building complex page layouts.

If you're looking for custom UI themes support please take a look at custom themes support in Spring MVC.

Jk1
  • 11,233
  • 9
  • 54
  • 64
3

I have used Apache Tiles. It can be very easily integrated with Spring MVC. You can also consider using sitemesh. As you mentioned header template, footer template you can have base template for header, footer, menu and you can integrate them into base template,

Note : You can only have jsp and not html here.

Check this link for spring with tiles.

http://www.springbyexample.org/examples/dynamic-tiles-spring-mvc-module.html

Lucky
  • 16,787
  • 19
  • 117
  • 151
pappu_kutty
  • 2,378
  • 8
  • 49
  • 93
  • 1
    Before using sitemash search for some comparism of tiles and sitemash in stackoverflow. For example http://stackoverflow.com/questions/8565029/apache-tiles-alternatives – Ralph Oct 13 '13 at 17:15
1

If you want to seperate Header, Footer and some other common stuff from the "real" content, then you can use Apache Tiles.

It is easy to integrate in spring. So for example Spring Roo use it.

Ralph
  • 118,862
  • 56
  • 287
  • 383
0

Ideally yes, You need to create common files instead of redundant code because it might leads to many changes if u want to change a single thing. So try to use below code based on your requirement.

<%@ include is a static include, <jsp:include is a dynamic include.

Another solution: As you are using Spring framework try to use spring tiles search in google for more help for example SpringByExample

Raja P
  • 3
  • 6