Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added thymeleaf support

  • Loading branch information...
commit 3a58e12b168404145b6da889e5e4b89a495fbbbd 1 parent 3ef3457
Mic authored
View
2  .springBeans
@@ -7,7 +7,7 @@
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
- <config>src/main/webapp/WEB-INF/mvc-config.xml</config>
+ <config>src/main/webapp/WEB-INF/spring-mvc-config.xml</config>
</configs>
<configSets>
</configSets>
View
13 pom.xml
@@ -16,6 +16,7 @@
<tiles.version>2.2.2</tiles.version>
<sl4j.version>1.7.0</sl4j.version>
<log4j.version>1.2.17</log4j.version>
+ <thymeleaf.version>2.0.13</thymeleaf.version>
</properties>
<build>
@@ -98,6 +99,12 @@
</dependency>
<dependency>
+ <groupId>org.apache.tiles</groupId>
+ <artifactId>tiles-extras</artifactId>
+ <version>${tiles.version}</version>
+ </dependency>
+
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
@@ -131,6 +138,12 @@
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.thymeleaf</groupId>
+ <artifactId>thymeleaf-spring3</artifactId>
+ <version>${thymeleaf.version}</version>
+ </dependency>
View
10 src/main/java/controller/UserController.java
@@ -17,20 +17,30 @@
@RequestMapping(value="/users/all/jsp-plain",method=RequestMethod.GET)
public String findUsersPlain(Model model){
buildUserList(model);
+ model.addAttribute("title", "Users List - Plain JSP");
return "01-plain/users";
}
@RequestMapping(value="/users/all/custom-tags",method=RequestMethod.GET)
public String findUsersTags(Model model){
buildUserList(model);
+ model.addAttribute("title", "Users List - Custom tags");
return "02-custom-tags/users";
}
@RequestMapping(value="/users/all/tiles",method=RequestMethod.GET)
public String findUsersTiles(Model model){
buildUserList(model);
+ model.addAttribute("title", "Users List - Tiles");
return "tiles/users";
}
+
+ @RequestMapping(value="/users/all/thymeleaf",method=RequestMethod.GET)
+ public String findUsersThymeLeaf(Model model){
+ buildUserList(model);
+ model.addAttribute("title", "Users List - Thymeleaf");
+ return "thymeleaf/users";
+ }
private void buildUserList(Model model) {
List<User> users = new ArrayList<User>();
View
23 src/main/webapp/WEB-INF/spring-mvc-config.xml
@@ -0,0 +1,23 @@
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:mvc="http://www.springframework.org/schema/mvc"
+ xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+
+ <!-- Scans the classpath of this application for @Components to deploy as beans -->
+ <context:component-scan base-package="controller" />
+
+ <!-- Configures the @Controller programming model -->
+ <mvc:annotation-driven />
+
+ <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
+ <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
+ <property name="prefix" value="/WEB-INF/view/jsp/"/>
+ <property name="suffix" value=".jsp"/>
+ <property name="order" value="2"/>
+ </bean>
+
+</beans>
View
24 src/main/webapp/WEB-INF/spring-thymeleaf-config.xml
@@ -0,0 +1,24 @@
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:mvc="http://www.springframework.org/schema/mvc"
+ xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+
+ <bean id="templateResolver"
+ class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
+ <property name="prefix" value="/WEB-INF/view/" />
+ <property name="suffix" value=".html" />
+ <property name="templateMode" value="HTML5" />
+ <property name="cacheable" value="false" />
+ </bean>
+ <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
+ <property name="templateResolver" ref="templateResolver" />
+ </bean>
+ <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
+ <property name="templateEngine" ref="templateEngine" />
+ <property name="order" value="1" />
+ </bean>
+
+</beans>
View
2  src/main/webapp/WEB-INF/view/jsp/01-plain/users.jsp
@@ -1 +1 @@
-<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
+<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
View
2  src/main/webapp/WEB-INF/view/jsp/02-custom-tags/users.jsp
@@ -1 +1 @@
-<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
+<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
View
6 src/main/webapp/WEB-INF/view/jsp/03-tiles/standard-layout.jsp
@@ -5,17 +5,17 @@
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
-<html>
+<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<spring:url value="/style" var="styleUrl" />
<link href="${styleUrl}/app.css" rel="stylesheet">
- <title><tiles:importAttribute name="title" /></title>
+ <title>${title}</title>
</head>
<body>
<div class="container" style="padding-top: 50px;">
<jsp:include page="/WEB-INF/view/jsp/menu.jsp"/>
- <tiles:insertAttribute name="main" />
+ <tiles:insertAttribute name="main" />
<jsp:include page="/WEB-INF/view/jsp/footer.jsp"/>
</div>
</body>
View
8 src/main/webapp/WEB-INF/view/jsp/03-tiles/tiles.xml
@@ -1,14 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
- "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
- "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
+ "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
- <definition name="standardLayout" template="/WEB-INF/view/jsp/03-tiles/standard-layout.jsp" />
-
- <definition name="tiles/*" extends="standardLayout">
- <put-attribute name="title" value="User List - Apache Tiles" />
+ <definition name="tiles/*" template="/WEB-INF/view/jsp/03-tiles/standard-layout.jsp">
<put-attribute name="main" value="/WEB-INF/view/jsp/03-tiles/{1}.jsp" />
</definition>
View
2  src/main/webapp/WEB-INF/view/jsp/03-tiles/users.jsp
@@ -1 +1 @@
-<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
+<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
View
7 src/main/webapp/WEB-INF/view/jsp/menu.jsp
@@ -1,16 +1,19 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+
<c:url value="/users/all/jsp-plain.htm" var="usersPlain" />
<c:url value="/users/all/custom-tags.htm" var="usersCustomTags" />
<c:url value="/users/all/tiles.htm" var="usersTiles" />
+<c:url value="/users/all/thymeleaf.htm" var="usersThymeleaf" />
<c:url value="/images/springsource_banner_green.png" var="headerImage" />
<div class="header well">
- <img src="${headerImage}">
+ <img src="${headerImage}"/>
<div class="page-header">
- <h1>MVC Layout Samples</h1>
+ <h1>${title}</h1>
</div>
<ul>
<li><a href="${usersPlain}">No template</a></li>
<li><a href="${usersCustomTags}">Custom tags</a></li>
<li><a href="${usersTiles}">Apache Tiles</a></li>
+ <li><a href="${usersThymeleaf}">Thymeleaf</a></li>
</ul>
</div>
View
24 src/main/webapp/WEB-INF/view/thymeleaf/menu.html
@@ -0,0 +1,24 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:th="http://www.thymeleaf.org">
+<head id="headerFragment" th:fragment="headerFragment">
+
+<div class="header well">
+ <img th:src="@{/images/springsource_banner_green.png}"/>
+ <link th:src="@{/style/app.css}" rel="stylesheet"/>
+ <div class="page-header">
+ <h1 th:text="${title}"/>
+ </div>
+ <ul>
+ <li><a th:href="@{/users/all/jsp-plain.htm}">No template</a></li>
+ <li><a th:href="@{/users/all/custom-tags.htm}">Custom tags</a></li>
+ <li><a th:href="@{/users/all/tiles.htm}">Apache Tiles</a></li>
+ <li><a th:href="@{/users/all/thymeleaf.htm}">Thymeleaf</a></li>
+ </ul>
+</div>
+
+</head>
+<body>
+ <div class="container">
+ </div>
+</body>
+</html>
View
28 src/main/webapp/WEB-INF/view/thymeleaf/users.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html xmlns:th="http://www.thymeleaf.org">
+ <head th:include="thymeleaf/menu :: headerFragment">
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>${title}</title>
+ </head>
+ <body>
+ <div th:if="${#lists.isEmpty(users)}">
+ Table is empty.
+ </div>
+ <div th:if="${not #lists.isEmpty(users)}">
+ <table>
+ <thead>
+ <tr>
+ <th> First Name </th>
+ <th> Last name </th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr th:each="user : ${users}">
+ <td th:text="${user.firstName}">John</td>
+ <td th:text="${user.lastName}">Smith</td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ </body>
+</html>
View
2  src/main/webapp/WEB-INF/web.xml
@@ -7,7 +7,7 @@
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
- /WEB-INF/mvc-config.xml, /WEB-INF/spring-tiles-config.xml
+ /WEB-INF/spring-*-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
Please sign in to comment.
Something went wrong with that request. Please try again.