Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add .jsp headers and footers to my Spring MVC web app?

How do I add .jsp headers and footers to my Spring MVC web app?

I know there's many different answers, but I would like to know (them all really but more importantly) what is the proper way to do this? I'm just learning Spring and I have a hint the answer lies with HandlerInterceptor. For now, I might just do so .jsp includes. Even with this include solution, could you detail where I would place the headers/footers structurally? Any advice or direction would be great.

like image 689
Xonatron Avatar asked Feb 23 '12 01:02

Xonatron


1 Answers

I found your question whilst researching :-) Not sure if my solution is <good | bad | a hack | already exists> or if there is a better way but it's working for my current project.

In your myapp-servlet.xml you can extend the viewResolver viewClass with your own implementation:

myapp-servlet.xml

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:viewClass="com.my.app.view.JstlView"
  p:prefix="/WEB-INF/views/"
  p:suffix=".jsp"/>

By overriding the renderMergedOutputModel you can force all views to really be a template in which you can define your global layout and then simply <jsp:include/> your partial(s).

JstlView.java

package com.my.app.view;

import java.util.*;
import org.springframework.web.servlet.view.InternalResourceView;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JstlView extends InternalResourceView {
    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String dispatcherPath = prepareForRendering(request, response);

        // set original view being asked for as a request parameter
        request.setAttribute("partial", dispatcherPath.substring(dispatcherPath.lastIndexOf("/") + 1);

        // force everything to be template.jsp
        RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/template.jsp");
        rd.include(request, response);
    }
}

If you look Spring's InternalResourceView.java you'll get a better idea of what Spring is doing when determining what view to use.

template.jsp

<!doctype html>
<html lang="en">
<head></head>
<body>
    <header>
        <jsp:include page="header.jsp"/>
    </header>
    <jsp:include page="${partial}"/>
    <footer>
        <jsp:include page="footer.jsp"/>
    </footer>
</body>
</html>

How to obtain request / session / servletcontext attribute in JSP using EL? helped me here with getting the attribute value ${partial} out.

simple_partial.jsp

<p>I'm a partial!</p>

Then in a controller, return the simple_partial view

App.java

package com.my.app.controller;

import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/")
public class App{
    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "simple_partial";
    }
}

which will get wrapped by the template.jsp and responded out.

like image 54
andyb Avatar answered Oct 23 '22 22:10

andyb