Skip to content

Commit 7d5fd81

Browse files
author
Eugen
authored
Merge pull request eugenp#673 from kirangonella/master
Spring integration with Tiles
2 parents 25ea6ef + d98f232 commit 7d5fd81

14 files changed

Lines changed: 325 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<module>spring-mvc-java</module>
9797
<module>spring-mvc-no-xml</module>
9898
<module>spring-mvc-xml</module>
99+
<module>spring-mvc-tiles</module>
99100
<module>spring-openid</module>
100101
<module>spring-protobuf</module>
101102
<module>spring-quartz</module>

‎spring-mvc-tiles/pom.xml‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-mvc-tiles</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
<name>spring-mvc-tiles</name>
9+
<description>Integrating Spring MVC with Apache Tiles</description>
10+
11+
<properties>
12+
<springframework.version>4.3.2.RELEASE</springframework.version>
13+
<apachetiles.version>3.0.5</apachetiles.version>
14+
</properties>
15+
16+
<dependencies>
17+
<!-- Spring -->
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-core</artifactId>
21+
<version>${springframework.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework</groupId>
25+
<artifactId>spring-web</artifactId>
26+
<version>${springframework.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-webmvc</artifactId>
31+
<version>${springframework.version}</version>
32+
</dependency>
33+
<!-- Apache Tiles -->
34+
<dependency>
35+
<groupId>org.apache.tiles</groupId>
36+
<artifactId>tiles-jsp</artifactId>
37+
<version>${apachetiles.version}</version>
38+
</dependency>
39+
40+
<!-- Servlet+JSP+JSTL -->
41+
<dependency>
42+
<groupId>javax.servlet</groupId>
43+
<artifactId>javax.servlet-api</artifactId>
44+
<version>3.1.0</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>javax.servlet.jsp</groupId>
48+
<artifactId>javax.servlet.jsp-api</artifactId>
49+
<version>2.3.1</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>javax.servlet</groupId>
53+
<artifactId>jstl</artifactId>
54+
<version>1.2</version>
55+
</dependency>
56+
57+
</dependencies>
58+
59+
<build>
60+
<pluginManagement>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<version>3.2</version>
66+
<configuration>
67+
<source>1.7</source>
68+
<target>1.7</target>
69+
</configuration>
70+
</plugin>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-war-plugin</artifactId>
74+
<version>2.4</version>
75+
<configuration>
76+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
77+
<warName>spring-mvc-tiles</warName>
78+
<failOnMissingWebXml>false</failOnMissingWebXml>
79+
</configuration>
80+
</plugin>
81+
</plugins>
82+
</pluginManagement>
83+
<finalName>spring-mvc-tiles</finalName>
84+
</build>
85+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.tiles.springmvc;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
8+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
11+
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
12+
13+
@Configuration
14+
@EnableWebMvc
15+
@ComponentScan(basePackages = "com.baeldung.tiles.springmvc")
16+
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
17+
18+
/**
19+
* Configure TilesConfigurer.
20+
*/
21+
@Bean
22+
public TilesConfigurer tilesConfigurer() {
23+
TilesConfigurer tilesConfigurer = new TilesConfigurer();
24+
tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/views/**/tiles.xml" });
25+
tilesConfigurer.setCheckRefresh(true);
26+
return tilesConfigurer;
27+
}
28+
29+
/**
30+
* Configure ViewResolvers to deliver views.
31+
*/
32+
@Override
33+
public void configureViewResolvers(ViewResolverRegistry registry) {
34+
TilesViewResolver viewResolver = new TilesViewResolver();
35+
registry.viewResolver(viewResolver);
36+
}
37+
38+
/**
39+
* Configure ResourceHandlers to serve static resources
40+
*/
41+
42+
@Override
43+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
44+
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
45+
}
46+
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.tiles.springmvc;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.ModelMap;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
8+
@Controller
9+
@RequestMapping("/")
10+
public class ApplicationController {
11+
12+
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
13+
public String homePage(ModelMap model) {
14+
return "home";
15+
}
16+
17+
@RequestMapping(value = { "/apachetiles" }, method = RequestMethod.GET)
18+
public String productsPage(ModelMap model) {
19+
return "apachetiles";
20+
}
21+
22+
@RequestMapping(value = { "/springmvc" }, method = RequestMethod.GET)
23+
public String contactUsPage(ModelMap model) {
24+
return "springmvc";
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.tiles.springmvc;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return new Class[] { ApplicationConfiguration.class };
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return null;
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[] { "/" };
20+
}
21+
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Apache Tiles</title>
8+
</head>
9+
<body>
10+
<h2>Tiles with Spring MVC Demo</h2>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Home</title>
8+
</head>
9+
<body>
10+
<h2>Welcome to Apache Tiles integration with Spring MVC</h2>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Spring MVC</title>
8+
</head>
9+
<body>
10+
<h2>Spring MVC configured to work with Apache Tiles</h2>
11+
</body>
12+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<%@ page isELIgnored="false"%>
4+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
5+
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
6+
7+
<html>
8+
9+
<head>
10+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
11+
<title><tiles:getAsString name="title" /></title>
12+
<link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link>
13+
</head>
14+
15+
<body>
16+
<div class="flex-container">
17+
<tiles:insertAttribute name="header" />
18+
<tiles:insertAttribute name="menu" />
19+
<article class="article">
20+
<tiles:insertAttribute name="body" />
21+
</article>
22+
<tiles:insertAttribute name="footer" />
23+
</div>
24+
</body>
25+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
<footer>copyright © Baeldung</footer>

0 commit comments

Comments
 (0)