Showing posts with label spring mvc template. Show all posts
Showing posts with label spring mvc template. Show all posts

Sunday, September 14, 2014

Spring MVC Framework Global Layout Used To Be Template

In your xxxxx-servlet.xml you can extend the viewResolver viewClass with your own implementation (Download full source code)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="caseSensitive" value="true" />
        <!--<property name="pathPrefix" value="/pathPrefix" />-->
    </bean>


    <bean class="com.pkm.controllers.UserController"/>
    
    <bean id="viewResolver"
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
        <property name="viewClass">
            <value>com.pkm.common.JstlView</value>
        </property>
    </bean>

</beans>

JstlView.java

package com.pkm.common;

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

public class JstlView extends InternalResourceView {
    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String dispatcherPath = prepareForRendering(request, response);
        if (model != null) {
            for (Map.Entry pairs : model.entrySet()) {
                String key = pairs.getKey().toString();
                Object value = pairs.getValue();
                request.setAttribute(key, value);
            }
        }

        // set original view being asked for as a request parameter
        request.setAttribute("_____CONTROLLER_VIEW_NAME_____", dispatcherPath);

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

template.jsp


<!doctype html>
<html lang="en">
<head>
    <title>Hello :: Spring Application</title>
</head>
<body>
    <header>
        <jsp:include page="header.jsp"/>
    </header>
    <jsp:include page="${_____CONTROLLER_VIEW_NAME_____}"/>    <footer>
        <jsp:include page="footer.jsp"/>
    </footer>
</body>
</html>