This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP templating body content  RSS feed

 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have few JSP pages which have common header and side menu. I want to template the body content and change it according to the side menu which has been chosen. I have done this earlier but I forgot how to do it. I know we can use include tag or jsp:include or tag files. I just want the idea of, say if dashboard is selected, I have to show dashboard.jsp inside the main page. Any refresher hint will be helpful.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the include action to include the common header and footer on each page. Or, you could use a templating engine such as Tiles. Or, you could use Ajax to fetch the body content and inject it into the page.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,
Can you give me an example of how to do it in ajax?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a div, upon navigation perform an Ajax call to the page controller for the body fragment, which in turn forwards to its JSP and returns the generated HTML fragment as the response. Inject the response into the div.

This can be done in the page with a singe line of JavaScript using jQuery's .load() method.

Be aware that doing it this way makes bookmarking the page impossible.

If you are going to go down the JavaScript route, I'd create an SPA using a framework that provides URL routing so that browser controls (back, forward, refresh, bookmarks, etc) work as expected.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,

Thanks for the response. I will try your solution but as you said back, front, refresh or bookmark won't work. I am not aware of SPA but I am using Spring. So if you can please provide me links to learn about SPA and how to implement it in Spring.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An SPA (Single Page Application) usually uses a RESTful backend, which I suppose you could use Spring for. On the client, SPAs use a JavaScript MVC framework.

A good resource might be this book

If you don't want to get involved with a JavaScript SPA framework, and want to do a traditional web application using something like SpringMVC and JSPs, then you should likely just go the include route.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!