Umstellung auf Thymeleaf: Schritt 1 - Thymeleaf zum Laufen bekommen
authorKai Moritz <km@juplo.de>
Sat, 30 Aug 2014 20:21:35 +0000 (22:21 +0200)
committerKai Moritz <kai@juplo.de>
Tue, 19 Jan 2016 16:45:42 +0000 (17:45 +0100)
 * Tiles (vorerst) rausgeschmissen
 * Thymeleaf so weit konfiguriert, dass eine View /index.html erreichbar ist

12 files changed:
pom.xml
src/main/resources/log4j.xml
src/main/resources/spring/mvc.xml
src/main/webapp/WEB-INF/web.xml
src/main/webapp/thymeleaf/404.html [new file with mode: 0644]
src/main/webapp/thymeleaf/error.html [new file with mode: 0644]
src/main/webapp/thymeleaf/index.html [new file with mode: 0644]
src/main/webapp/thymeleaf/templates/footer.html [new file with mode: 0644]
src/main/webapp/thymeleaf/templates/header.html [new file with mode: 0644]
src/main/webapp/thymeleaf/views/404.html [new file with mode: 0644]
src/main/webapp/thymeleaf/views/error.html [new file with mode: 0644]
src/main/webapp/thymeleaf/views/index.html [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index af8665d..fd91c8d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -42,9 +42,9 @@
     <jsp-api.version>2.2.1</jsp-api.version>
     <jstl.version>1.2</jstl.version>
     <servlet-api.version>3.0.1</servlet-api.version>
-    <slf4j.version>1.5.8</slf4j.version>
+    <slf4j.version>1.6.1</slf4j.version>
     <springframework.version>4.0.6.RELEASE</springframework.version>
-    <tiles.version>3.0.1</tiles.version>
+    <thymeleaf.version>2.1.3.RELEASE</thymeleaf.version>
     <wro4j.version>1.7.3-SNAPSHOT</wro4j.version>
 
   </properties>
       </exclusions>
     </dependency>
 
-    <!-- Tiles -->
+    <!-- Thymeleaf -->
     <dependency>
-      <groupId>org.apache.tiles</groupId>
-      <artifactId>tiles-core</artifactId>
-      <version>${tiles.version}</version>
+      <groupId>org.thymeleaf</groupId>
+      <artifactId>thymeleaf</artifactId>
+      <version>${thymeleaf.version}</version>
       <scope>runtime</scope>
     </dependency>
     <dependency>
-      <groupId>org.apache.tiles</groupId>
-      <artifactId>tiles-jsp</artifactId>
-      <version>${tiles.version}</version>
+      <groupId>org.thymeleaf</groupId>
+      <artifactId>thymeleaf-spring4</artifactId>
+      <version>${thymeleaf.version}</version>
       <scope>runtime</scope>
     </dependency>
 
index b6030f9..a3d7d44 100644 (file)
@@ -16,6 +16,9 @@
   <logger name="org.springframework">
     <level value="debug" />
   </logger>
+  <logger name="org.thymeleaf">
+    <level value="debug" />
+  </logger>
 
   <root>
     <level value="info"/>
index a18d739..e2cdc38 100644 (file)
@@ -22,6 +22,7 @@
   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="mappings">
       <value>
+        /thymeleaf/**=staticResources
         /*.html=urlFilenameViewController
         /**/*.html=urlFilenameViewController
       </value>
     <property name="order" value="10"/>
   </bean>
   <bean id="urlFilenameViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
+  <bean id="staticResources" class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
+    <property name="locations">
+      <list>
+        <value>/thymeleaf/</value>
+      </list>
+    </property>
+  </bean>
 
-  <!-- Tiles View-Resolver -->
-  <bean class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/>
+  <!-- Thymeleaf Spring-View-Resolver -->
+  <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
+    <property name="templateEngine" ref="templateEngine" />
+  </bean>
 
-  <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
-  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
-    <property name="prefix" value="/WEB-INF/views/"/>
-    <property name="suffix" value=".jsp"/>
+
+  <!-- Thymeleaf Template-Resolver -->
+  <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
+    <property name="prefix" value="/thymeleaf/" />
+    <property name="suffix" value=".html" />
+    <property name="templateMode" value="HTML5" />
+    <property name="cacheable" value="false" /><!-- Only for development -->
   </bean>
 
-  <bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
-    <property name="definitions">
-      <list>
-        <value>classpath:/spring/tiles.xml</value>
-      </list>
-    </property>
-    <property name="useMutableTilesContainer" value="true"/>
+  <!-- Thymeleaf Spring4-Template-Engine -->
+  <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
+    <property name="templateResolver" ref="templateResolver" />
   </bean>
 
 </beans>
index b3fcbbf..89be328 100644 (file)
 
   <servlet-mapping>
     <servlet-name>Dispatcher Servlet - Branding</servlet-name>
-    <url-pattern>*.html</url-pattern>
+    <url-pattern>/</url-pattern>
   </servlet-mapping>
 
   <error-page>
     <error-code>404</error-code>
-    <location>/WEB-INF/404.jsp</location>
+    <location>/404.html</location>
   </error-page>
   <error-page>
-    <location>/WEB-INF/error.jsp</location>
+    <location>/error.html</location>
   </error-page>
 
 </web-app>
diff --git a/src/main/webapp/thymeleaf/404.html b/src/main/webapp/thymeleaf/404.html
new file mode 100644 (file)
index 0000000..ab9c294
--- /dev/null
@@ -0,0 +1,14 @@
+<!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
new file mode 100644 (file)
index 0000000..9084f7f
--- /dev/null
@@ -0,0 +1,14 @@
+<!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
new file mode 100644 (file)
index 0000000..7f29820
--- /dev/null
@@ -0,0 +1,14 @@
+<!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/footer.html b/src/main/webapp/thymeleaf/templates/footer.html
new file mode 100644 (file)
index 0000000..32d3ffc
--- /dev/null
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title>Footer-Template</title>
+  </head>
+  <body>
+    <footer th:fragment="fragment">
+      <hr />
+      &copy; 2014 juplo NG
+    </footer>
+  </body>
+</html>
\ No newline at end of file
diff --git a/src/main/webapp/thymeleaf/templates/header.html b/src/main/webapp/thymeleaf/templates/header.html
new file mode 100644 (file)
index 0000000..a237023
--- /dev/null
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title>Header-Template</title>
+  </head>
+  <body>
+    <header th:fragment="fragment">
+      juplo proudly presemts:
+      <hr />
+    </header>
+  </body>
+</html>
\ No newline at end of file
diff --git a/src/main/webapp/thymeleaf/views/404.html b/src/main/webapp/thymeleaf/views/404.html
new file mode 100644 (file)
index 0000000..1848382
--- /dev/null
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title th:fragment="title">404: Page not found</title>
+  </head>
+  <body>
+    <div th: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>
+      <p>Really.</p>
+    </div>
+  </body>
+</html>
diff --git a/src/main/webapp/thymeleaf/views/error.html b/src/main/webapp/thymeleaf/views/error.html
new file mode 100644 (file)
index 0000000..ad0457a
--- /dev/null
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title th:fragment="title">An unexpected Error occured!</title>
+  </head>
+  <body>
+    <div th:fragment="content">
+      <p>We do not know, what happend.</p>
+      <p>But we can tell you: WE ARE SORRY!</p>
+      <p>Really.</p>
+    </div>
+  </body>
+</html>
diff --git a/src/main/webapp/thymeleaf/views/index.html b/src/main/webapp/thymeleaf/views/index.html
new file mode 100644 (file)
index 0000000..b44963c
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title th:fragment="title">Index</title>
+  </head>
+  <body>
+    <div th:fragment="content">
+      <p>Inhalt der Index-Seite...</p>
+    </div>
+  </body>
+</html>