Spring Tiles

Working sample: https://github.com/teachkoka/spring-tiles

 

mvc-dispatcher-servlet.xml

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="order" value="0"/>
    <property name="viewClass">
        <value>
            org.springframework.web.servlet.view.tiles3.TilesView
        </value>
    </property>
</bean>

<bean id="tilesConfigurer"
      class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

tiles.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

<tiles-definitions>
     <definition name="base.defination" template="/WEB-INF/pages/layout.jsp">
         <put-attribute name="title" value=""/>
         <put-attribute name="header" value="/WEB-INF/pages/header.jsp"/>
         <put-attribute name="menu" value="/WEB-INF/pages/menu.jsp"/>
         <put-attribute name="body" value=""/>
         <put-attribute name="footer" value="/WEB-INF/pages/footer.jsp" />
     </definition>

     <definition name="student-login" extends="base.defination">
         <put-attribute name="title" value="student login"/>
         <put-attribute name="body" value="/WEB-INF/pages/student-login.jsp"/>
     </definition>

    <definition name="result/success" extends="base.defination">
        <put-attribute name="title" value="success page"/>
        <put-attribute name="body" value="/WEB-INF/pages/result/success.jsp"/>
    </definition>

</tiles-definitions>

 

AppConfig

package com.b8.spring.javaconfig.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;

@Configuration
@ComponentScan(basePackages={"com.b8.spring.javaconfig"})
@EnableWebMvc
public class AppConfig {
	
	@Bean
	public DataSource dataSource(){
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("org.postgresql.Driver");
		dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/postgres");
		dataSource.setUsername("postgres");
		dataSource.setPassword("abcd12345");
		return dataSource;
	}
	
	@Bean
	public JdbcTemplate jdbcTemplate(){
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
		return jdbcTemplate;
	}
	
	@Bean
	public TilesConfigurer getTilesConfigurer() {
		TilesConfigurer tilesConfigurer = new TilesConfigurer();

		tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/tiles.xml" });
		tilesConfigurer.setCheckRefresh(true);
		return tilesConfigurer;
	}

	@Bean
	public UrlBasedViewResolver viewResolver(){
		UrlBasedViewResolver ubvr = new UrlBasedViewResolver();
		ubvr.setOrder(0);
		ubvr.setViewClass(org.springframework.web.servlet.view.tiles3.TilesView.class);
		return ubvr;
	}
	
/*	@Bean
	public InternalResourceViewResolver viewResolver(){
		InternalResourceViewResolver irvr = new InternalResourceViewResolver();
		irvr.setPrefix("/WEB-INF/pages/");
		irvr.setSuffix(".jsp");
		return irvr;
	}*/
	
	@Bean
	public ResourceBundleMessageSource messageSource(){
		ResourceBundleMessageSource rbms = new ResourceBundleMessageSource();
		rbms.setBasename("messages");
		return rbms;
	}

	
}

AppInitializer.java

package com.b8.spring.javaconfig.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class AppInitializeer implements WebApplicationInitializer{

	public void onStartup(ServletContext servletContext)
			throws ServletException {
		
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.register(AppConfig.class);
		
		servletContext.addListener(new ContextLoaderListener(context));
		
		Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
		servlet.addMapping("/");
		servlet.setLoadOnStartup(1);
	}

}

 

 

Layout.jsp

<table border="1" cellpadding="2" cellspacing="2" align="center">
  <tr>
    <td height="30" colspan="2">
      <tiles:insertAttribute name="header"/>
    </td>
  </tr>
  <tr>
     <td height="250"><tiles:insertAttribute name="menu"/></td>
     <td width="350"><tiles:insertAttribute name="body"/> </td>
  </tr>
  <tr>

  </tr><td height="30" colspan="2">
    <tiles:insertAttribute name="footer"/>
</td>

</table>

 

BootStrap

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <div class="table-responsive">
        <table class="table" >
            <tr>
                <td height="100" colspan="2"  style="background-color: Azure; border: thick;font: bold;">
                    <tiles:insertAttribute name="header"/>
                </td>
            </tr>
            <tr>
                <td height="250"  width="200" style="background-color:BurlyWood; border: thick;font: bold;">
                    <tiles:insertAttribute name="menu"/>
                </td>
                <td height="350" width="400" style="background-color: lightblue; border: thick;font: bold;">
                  <%--  <tiles:insertAttribute name="title" /><br>--%>
                    <tiles:insertAttribute name="body"/>
                </td>
            </tr>
            <tr>
                <td height="100"  colspan="2" style="background-color: Azure; border: thick;font: bold;">
                    <tiles:insertAttribute name="footer"/>
                </td>
            </tr>
        </table>
    </div>
</div>

</body>
</html>