Umstellung auf Thymeleaf: Schritt 2 - Tilesunterstützung aktiviert
authorKai Moritz <km@juplo.de>
Sun, 31 Aug 2014 17:07:10 +0000 (19:07 +0200)
committerKai Moritz <kai@juplo.de>
Tue, 19 Jan 2016 16:45:42 +0000 (17:45 +0100)
pom.xml
src/main/resources/spring/mvc.xml
src/main/resources/spring/tiles.xml
src/main/webapp/thymeleaf/404.html [deleted file]
src/main/webapp/thymeleaf/error.html [deleted file]
src/main/webapp/thymeleaf/index.html [deleted file]
src/main/webapp/thymeleaf/templates/layout.html [new file with mode: 0644]
src/main/webapp/thymeleaf/views/404.html
src/main/webapp/thymeleaf/views/error.html
src/main/webapp/thymeleaf/views/index.html

diff --git a/pom.xml b/pom.xml
index fd91c8d..cf5f668 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -45,6 +45,8 @@
     <slf4j.version>1.6.1</slf4j.version>
     <springframework.version>4.0.6.RELEASE</springframework.version>
     <thymeleaf.version>2.1.3.RELEASE</thymeleaf.version>
+    <thymeleaf-extras.version>2.1.1.RELEASE</thymeleaf-extras.version>
+    <tiles.version>2.2.2</tiles.version>
     <wro4j.version>1.7.3-SNAPSHOT</wro4j.version>
 
   </properties>
       <version>${thymeleaf.version}</version>
       <scope>runtime</scope>
     </dependency>
+    <dependency>
+      <groupId>org.thymeleaf.extras</groupId>
+      <artifactId>thymeleaf-extras-tiles2</artifactId>
+      <version>${thymeleaf-extras.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.thymeleaf</groupId>
+          <artifactId>thymeleaf</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.thymeleaf</groupId>
+          <artifactId>thymeleaf-spring4</artifactId>
+        </exclusion>
+      </exclusions>
+      <scope>runtime</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.thymeleaf.extras</groupId>
+      <artifactId>thymeleaf-extras-tiles2-spring4</artifactId>
+      <version>${thymeleaf-extras.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.thymeleaf</groupId>
+          <artifactId>thymeleaf</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.thymeleaf</groupId>
+          <artifactId>thymeleaf-spring4</artifactId>
+        </exclusion>
+      </exclusions>
+      <scope>runtime</scope>
+    </dependency>
+
+    <!-- Tiles -->
+    <dependency>
+      <groupId>org.apache.tiles</groupId>
+      <artifactId>tiles-core</artifactId>
+      <version>${tiles.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.slf4j</groupId>
+          <artifactId>jcl-over-slf4j</artifactId>
+        </exclusion>
+      </exclusions>
+      <scope>runtime</scope>
+    </dependency>
 
     <!-- Servlet -->
     <dependency>
       <version>${slf4j.version}</version>
       <scope>runtime</scope>
     </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jcl-over-slf4j</artifactId>
+      <version>${slf4j.version}</version>
+      <scope>runtime</scope>
+    </dependency>
 
   </dependencies>
 
index e2cdc38..87c2a01 100644 (file)
@@ -3,6 +3,7 @@
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
@@ -10,6 +11,8 @@
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
+           http://www.springframework.org/schema/util
+           http://www.springframework.org/schema/util/spring-util-4.0.xsd
            ">
 
   <!-- Damit das DispatcherServlet auf /* gemappt werden kann -->
   <!-- Thymeleaf Spring-View-Resolver -->
   <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
     <property name="templateEngine" ref="templateEngine" />
+    <property name="viewNames" value="views/*,templates/*"/>
+    <property name="order" value="1"/>
+  </bean>
+
+  <!-- Thymeleaf Tiles-View-Resolver -->
+  <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
+    <property name="templateEngine" ref="templateEngine" />
+    <property name="viewClass" value="org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView"/>
+    <property name="order" value="2"/>
+  </bean>
+
+
+  <!-- Thymeleaf Tiles-Configurer -->
+  <bean class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
+    <property name="definitions">
+      <list>
+        <value>classpath:/spring/tiles.xml</value>
+      </list>
+    </property>
+    <property name="useMutableTilesContainer" value="true"/>
   </bean>
 
 
   <!-- Thymeleaf Spring4-Template-Engine -->
   <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
     <property name="templateResolver" ref="templateResolver" />
+    <property name="additionalDialects">
+      <util:set>
+        <bean class="org.thymeleaf.extras.tiles2.dialect.TilesDialect"/>
+      </util:set>
+    </property>
   </bean>
 
 </beans>
index 120e604..d9dd9a6 100644 (file)
@@ -1,9 +1,26 @@
 <?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">
+       "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
+       "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
 <tiles-definitions>
 
+  <!--
+    Main definition that uses layout.html to put title and content attributes from
+    Thymeleaf fragments; {1} will be evaluated to either 'list', 'form' or 'view'.
+  -->
+  <definition name="*" template="templates/layout">
+    <put-attribute name="title" value="title/{1}"/>
+    <put-attribute name="content" value="content/{1}"/>
+  </definition>
+
+  <!--
+    Fragments that are included by messages/* definition above;
+    'template' attribute is a template file stored in /WEB-INF/views/message folder;
+    {1} will be evaluated to either 'list', 'form' or 'view'.
+  -->
+  <definition name="title/*" template="views/{1} :: title"/>
+  <definition name="content/*" template="views/{1} :: content"/>
+
   <definition name="BASEPAGE" template="/WEB-INF/templates/html5page.jsp">
     <put-attribute name="title" value="TITLE IS MISSING" type="string" cascade="true"/>
     <put-attribute name="htmlhead" value="/WEB-INF/templates/html-head.jsp" type="template"/>
diff --git a/src/main/webapp/thymeleaf/404.html b/src/main/webapp/thymeleaf/404.html
deleted file mode 100644 (file)
index ab9c294..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-<!DOCTYPE html>
-<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
-  <head>
-    <title th:replace="views/404 :: title">TITLE</title>
-    <meta charset="UTF-8"/>
-    <meta name="viewport" content="width=device-width"/>
-  </head>
-  <body>
-    <div th:replace="templates/header :: fragment">HEADER</div>
-    <h1 th:include="views/404 :: title">TITLE</h1>
-    <div th:replace="views/404 :: content">CONTENT</div>
-    <div th:replace="templates/footer :: fragment">FOOTER</div>
-  </body>
-</html>
diff --git a/src/main/webapp/thymeleaf/error.html b/src/main/webapp/thymeleaf/error.html
deleted file mode 100644 (file)
index 9084f7f..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-<!DOCTYPE html>
-<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
-  <head>
-    <title th:replace="views/error :: title">TITLE</title>
-    <meta charset="UTF-8"/>
-    <meta name="viewport" content="width=device-width"/>
-  </head>
-  <body>
-    <div th:replace="templates/header :: fragment">HEADER</div>
-    <h1 th:include="views/error :: title">TITLE</h1>
-    <div th:replace="views/error :: content">CONTENT</div>
-    <div th:replace="templates/footer :: fragment">FOOTER</div>
-  </body>
-</html>
diff --git a/src/main/webapp/thymeleaf/index.html b/src/main/webapp/thymeleaf/index.html
deleted file mode 100644 (file)
index 7f29820..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-<!DOCTYPE html>
-<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
-  <head>
-    <title th:replace="views/index :: title">TITLE</title>
-    <meta charset="UTF-8"/>
-    <meta name="viewport" content="width=device-width"/>
-  </head>
-  <body>
-    <div th:replace="templates/header :: fragment">HEADER</div>
-    <h1 th:include="views/index :: title">TITLE</h1>
-    <div th:replace="views/index :: content">CONTENT</div>
-    <div th:replace="templates/footer :: fragment">FOOTER</div>
-  </body>
-</html>
diff --git a/src/main/webapp/thymeleaf/templates/layout.html b/src/main/webapp/thymeleaf/templates/layout.html
new file mode 100644 (file)
index 0000000..f019b46
--- /dev/null
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  xmlns:tiles="http://www.thymeleaf.org">
+  <head>
+    <!--/* Tiles attribute will be put here at runtime */-->
+    <title tiles:include="title">Title</title>
+  </head>
+  <body>
+    <!--/* Standard layout can be mixed with Tiles */-->
+    <div th:replace="templates/header :: fragment">HEADER</div>
+    <div class="container">
+      <!--/* Tiles attribute will be put here at runtime */-->
+      <h1 tiles:include="title">Title</h1>
+      <!--/* Tiles attribute will be put here at runtime */-->
+      <div tiles:replace="content">
+        <!-- ============================================================================ -->
+        <!-- This content is only used for static prototyping purposes (natural templates)-->
+        <!-- and is therefore entirely optional, as this markup fragment will be included -->
+        <!-- from "fragments/header.html" at runtime.                                     -->
+        <!-- ============================================================================ -->
+        <h1>Content</h1>
+        <p>
+          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
+          Praesent scelerisque neque neque, ac elementum quam dignissim interdum.
+          Phasellus et placerat elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
+          Praesent scelerisque neque neque, ac elementum quam dignissim interdum.
+          Phasellus et placerat elit.
+        </p>
+      </div>
+    </div>
+    <!--/* Standard layout can be mixed with Tiles */-->
+    <div th:replace="templates/footer :: fragment">FOOTER</div>
+  </body>
+</html>
\ No newline at end of file
index 1848382..6c22532 100644 (file)
@@ -1,10 +1,10 @@
 <!DOCTYPE html>
-<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:tiles="http://www.thymeleaf.org">
   <head>
-    <title th:fragment="title">404: Page not found</title>
+    <title tiles:fragment="title">404: Page not found</title>
   </head>
   <body>
-    <div th:fragment="content">
+    <div tiles:fragment="content">
       <p>We do not know, where you are.</p>
       <p>And we do not know, why you are here.</p>
       <p>But we can tell you: WE ARE SORRY!</p>
index ad0457a..a07dc23 100644 (file)
@@ -1,10 +1,10 @@
 <!DOCTYPE html>
-<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:tiles="http://www.thymeleaf.org">
   <head>
-    <title th:fragment="title">An unexpected Error occured!</title>
+    <title tiles:fragment="title">An unexpected Error occured!</title>
   </head>
   <body>
-    <div th:fragment="content">
+    <div tiles:fragment="content">
       <p>We do not know, what happend.</p>
       <p>But we can tell you: WE ARE SORRY!</p>
       <p>Really.</p>
index b44963c..101bdb8 100644 (file)
@@ -1,10 +1,10 @@
 <!DOCTYPE html>
-<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:tiles="http://www.thymeleaf.org">
   <head>
-    <title th:fragment="title">Index</title>
+    <title tiles:fragment="title">Index</title>
   </head>
   <body>
-    <div th:fragment="content">
+    <div tiles:fragment="content">
       <p>Inhalt der Index-Seite...</p>
     </div>
   </body>