Spring Tiles

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

 

mvc-dispatcher-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ 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>