2

I have a complicated website design (downloaded a design + css from the web) and I want to cleverly use includes, so that I can separate the design from the content.

However, there's some complication with that. the content is located inside a <div> which is inside a <div>, etc. How do I use includes so that basically, I can have each repeatable aspect of the site (header, navigation) in its own file, and for every actual page, have none of the design in the jsp?

i.e., in a certain page, I only want to have

//possible includes
<h1>Hello World!</h1>
//possible includes
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
  • I was going to recommend [sitemesh](http://www.opensymphony.com/sitemesh/), which does pretty much exactly what you want. But it seems the group behind sitemesh had a bit of a disintegration, or something. – aroth Jul 05 '11 at 23:39
  • Any chance to use JSP's successor Facelets? You can see an example of whatever you want in the 2nd part of [this answer](http://stackoverflow.com/questions/4792862/how-to-include-another-xhtml-in-xhtml-using-jsf-2-0-facelets/4793959#4793959). You'll only have to put yourself open to adopting JSF as MVC framework. – BalusC Jul 06 '11 at 05:47
  • @BalusC this is for a university project so... no. – Amir Rachum Jul 06 '11 at 07:20
  • Ah right. Good luck with project :) – BalusC Jul 06 '11 at 07:20

1 Answers1

2

You should probably look at Apache Tiles since it does most of this for you.

If you want to roll your own you can create two files, say header.jsp and footer.jsp with the start and end of the page:

header.jsp

 <html>
   <head>
   </head>
   <body>
      <div>
        //header content
      </div>
      <div>
      //main content

and, footer.jsp

      </div>
    </body>
  </html>

And include both in your content pages.

Alternatively, you can create a single layout.jsp page:

  <html>
   <head>
   </head>
   <body>
      <div>
        //header content
      </div>
      <div>
      <c:out value="${content}"/>
      </div>
    </body>
  </html>

Then all request will populate the content variable then load the layout page. This way you do not have to repeat the includes all over the place.

Example: In your servlet you can give the variable a value like this:

        String content = "<h1>Hello World!</h1>";
        request.setAttribute("content", content);
        //forward to layout.jsp

This way, on the layout.jsp page the content will be displayed. You can of course create several such variable placeholders.

Third Option: Use a jsp:include tag to include dynamic content. For example The layout.jsp page will look like this:

    <html>
       <head>
       </head>
       <body>
         <div>
           //header content
         </div>
         <div>
           <jsp:include page="${page}"/>
         </div>
       </body>
    </html>

The include tag will fetch the page at the given url. The reason for using this suggestion is when you have more complicated displays it would be better to use a JSP page to construct the display. The servlet will then only be concerned with the business logic. You can create a separate jsp page for each display. The servlet will look like this:

    //business logic
    //save the data to be displayed in the request
    String page = "nextpagetodisplay.jsp";
    request.setAttribute("page", page);
    //forward to layout.jsp
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192