Umstellung auf Thymeleaf: Zwischenschritt - Fehler in Thymeleaf umschifft
authorKai Moritz <km@juplo.de>
Mon, 1 Sep 2014 10:12:10 +0000 (12:12 +0200)
committerKai Moritz <kai@juplo.de>
Tue, 19 Jan 2016 16:45:42 +0000 (17:45 +0100)
pom.xml
src/main/java/de/juplo/branding/web/ForceIncludeFilter.java [new file with mode: 0644]
src/main/resources/spring/tiles.xml
src/main/webapp/WEB-INF/views/tiles.jsp [new file with mode: 0644]
src/main/webapp/WEB-INF/web.xml
src/main/webapp/thymeleaf/templates/layout.html

diff --git a/pom.xml b/pom.xml
index cf5f668..256cbbc 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       </exclusions>
       <scope>runtime</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.tiles</groupId>
+      <artifactId>tiles-servlet</artifactId>
+      <version>${tiles.version}</version>
+    </dependency>
 
     <!-- Servlet -->
     <dependency>
diff --git a/src/main/java/de/juplo/branding/web/ForceIncludeFilter.java b/src/main/java/de/juplo/branding/web/ForceIncludeFilter.java
new file mode 100644 (file)
index 0000000..953e8b2
--- /dev/null
@@ -0,0 +1,55 @@
+package de.juplo.branding.web;
+
+import java.io.IOException;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.tiles.servlet.context.ServletUtil;
+
+
+
+/**
+ * Fixes a logic-bug in the colaboration of Thymeleaf and Tiles.
+ *
+ * Tiles 2.2.x tries to use RequestDispatcher.forward() whenever possible, but
+ * this breaks Thymeleaf, because the original response is marked as committed,
+ * when forwarding. Hence, Thymeleaf cannot render its result in the response
+ * and an empty page is returned.
+ * 
+ * @author kai
+ */
+public class ForceIncludeFilter implements Filter
+{
+
+  @Override
+  public void init(FilterConfig filterConfig) throws ServletException
+  {
+  }
+
+  @Override
+  public void doFilter(
+      ServletRequest request,
+      ServletResponse response,
+      FilterChain chain
+      )
+      throws
+        IOException,
+        ServletException
+  {
+    if ((response instanceof HttpServletResponse))
+      ServletUtil.setForceInclude((HttpServletRequest)request, true);
+    chain.doFilter(request, response);
+  }
+
+  @Override
+  public void destroy()
+  {
+  }
+  
+}
index d9dd9a6..8439ff0 100644 (file)
@@ -11,6 +11,7 @@
   <definition name="*" template="templates/layout">
     <put-attribute name="title" value="title/{1}"/>
     <put-attribute name="content" value="content/{1}"/>
+    <put-attribute name="tiles" value="/WEB-INF/views/tiles.jsp" type="jsp"/>
   </definition>
 
   <!--
diff --git a/src/main/webapp/WEB-INF/views/tiles.jsp b/src/main/webapp/WEB-INF/views/tiles.jsp
new file mode 100644 (file)
index 0000000..cee460f
--- /dev/null
@@ -0,0 +1,5 @@
+<%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>
+<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
+<c:forEach begin="1" end="10">
+Hello World!<br/>
+</c:forEach>
\ No newline at end of file
index 89be328..af11754 100644 (file)
@@ -21,6 +21,7 @@
 
 
   <!-- UTF-8 -->
+
   <filter>
     <filter-name>characterEncodingFilter</filter-name>
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   </filter-mapping>
 
 
+  <!-- Fix a logic-bug in the colaboration of Thymeleaf and Tiles -->
+
+  <filter>
+    <filter-name>forceIncludeFilter</filter-name>
+    <filter-class>de.juplo.branding.web.ForceIncludeFilter</filter-class>
+  </filter>
+
+  <filter-mapping>
+    <filter-name>forceIncludeFilter</filter-name>
+    <url-pattern>*.html</url-pattern>
+  </filter-mapping>
+
+
   <!-- Handles all requests into the application -->
 
   <servlet>
index f019b46..7d02e2b 100644 (file)
@@ -27,6 +27,8 @@
         </p>
       </div>
     </div>
+    <!--/* Tiles attribute will be put here at runtime */-->
+    <div tiles:replace="tiles">TILES</div>
     <!--/* Standard layout can be mixed with Tiles */-->
     <div th:replace="templates/footer :: fragment">FOOTER</div>
   </body>