Skip to content

Commit

Permalink
added thymeleaf support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic committed Sep 28, 2012
1 parent 3ef3457 commit 3a58e12
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .springBeans
Expand Up @@ -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>
Expand Down
13 changes: 13 additions & 0 deletions pom.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -97,6 +98,12 @@
<version>${tiles.version}</version>
</dependency>

<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>${tiles.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -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>



Expand Down
10 changes: 10 additions & 0 deletions src/main/java/controller/UserController.java
Expand Up @@ -17,20 +17,30 @@ public class UserController {
@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>();
Expand Down
23 changes: 23 additions & 0 deletions 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>
24 changes: 24 additions & 0 deletions 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>
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/view/jsp/01-plain/users.jsp
@@ -1 +1 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %><%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Users List</title> <spring:url value="/style" var="styleUrl" /> <link href="${styleUrl}/app.css" rel="stylesheet"> </head><body> <div class="container" style="padding-top: 50px;"> <jsp:include page="../menu.jsp"/> <table> <thead> <tr> <th> First Name </th> <th> Last name </th> </tr> </thead> <c:forEach var="user" items="${users}"> <tr> <td> ${user.firstName} </td> <td> ${user.lastName} </td> </tr> </c:forEach> </table> <jsp:include page="../footer.jsp"/> </div></body></html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %><%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>${title}</title> <spring:url value="/style" var="styleUrl" /> <link href="${styleUrl}/app.css" rel="stylesheet"> </head><body> <div class="container" style="padding-top: 50px;"> <jsp:include page="../menu.jsp"/> <c:choose> <c:when test="${empty users}"> Table is empty. </c:when> <c:otherwise> <table> <thead> <tr> <th> First Name </th> <th> Last name </th> </tr> </thead> <tbody> <c:forEach var="user" items="${users}"> <tr> <td> ${user.firstName} </td> <td> ${user.lastName} </td> </tr> </c:forEach> </tbody> </table> </c:otherwise> </c:choose> <jsp:include page="../footer.jsp"/> </div></body></html>
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/view/jsp/02-custom-tags/users.jsp
@@ -1 +1 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="layout" tagdir="/WEB-INF/tags" %><layout:mainLayout title="User List - Custom tag"> <table> <thead> <tr> <th> First Name </th> <th> Last name </th> </tr> </thead> <c:forEach var="user" items="${users}"> <tr> <td> ${user.firstName} </td> <td> ${user.lastName} </td> </tr> </c:forEach> </table></layout:mainLayout>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="layout" tagdir="/WEB-INF/tags" %><layout:mainLayout title="${title}/"> <c:choose> <c:when test="${empty users}"> Table is empty. </c:when> <c:otherwise> <table> <thead> <tr> <th> First Name </th> <th> Last name </th> </tr> </thead> <tbody> <c:forEach var="user" items="${users}"> <tr> <td> ${user.firstName} </td> <td> ${user.lastName} </td> </tr> </c:forEach> </tbody> </table> </c:otherwise> </c:choose></layout:mainLayout>
Expand Down
6 changes: 3 additions & 3 deletions src/main/webapp/WEB-INF/view/jsp/03-tiles/standard-layout.jsp
Expand Up @@ -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>
Expand Down
8 changes: 2 additions & 6 deletions 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>

Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/view/jsp/03-tiles/users.jsp
@@ -1 +1 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <table> <thead> <tr> <th> First Name </th> <th> Last name </th> </tr> </thead> <c:forEach var="user" items="${users}"> <tr> <td> ${user.firstName} </td> <td> ${user.lastName} </td> </tr> </c:forEach> </table>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:choose> <c:when test="${empty users}"> Table is empty. </c:when> <c:otherwise> <table> <thead> <tr> <th> First Name </th> <th> Last name </th> </tr> </thead> <tbody> <c:forEach var="user" items="${users}"> <tr> <td> ${user.firstName} </td> <td> ${user.lastName} </td> </tr> </c:forEach> </tbody> </table> </c:otherwise> </c:choose>
Expand Down
7 changes: 5 additions & 2 deletions 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>
24 changes: 24 additions & 0 deletions 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>
28 changes: 28 additions & 0 deletions 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>
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/web.xml
Expand Up @@ -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>
Expand Down

0 comments on commit 3a58e12

Please sign in to comment.