Repository navigation
Expand file tree
/
Copy pathpetclinic.html
More file actions
executable file
·530 lines (418 loc) · 22.9 KB
/
Copy pathpetclinic.html
File metadata and controls
executable file
·530 lines (418 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bringing Thymeleaf and Natural Templates to the Spring PetClinic - Thymeleaf: java XML/XHTML/HTML5 template engine</title>
<link rel="stylesheet" type="text/css" media="all" href="css/thymeleaf.css" />
<link rel="shortcut icon" href="http://www.thymeleaf.org/favicon.ico" />
<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
{lang:'en', parsetags:'explicit'}
</script>
<style type="text/css">
/* ---------------------- */
/* PETCLINIC IMAGES */
/* ---------------------- */
#content img {
display: block;
margin-left: auto;
margin-right: auto;
-moz-box-sizing: border-box;
box-sizing: border-box;
max-width: 100%;
padding-left: 40px;
padding-right: 40px;
}
</style>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1276954-9']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript" src="sh/scripts/shCore.js"></script>
<script type="text/javascript" src="sh/scripts/shBrushXml.js"></script>
<script type="text/javascript" src="sh/scripts/shBrushJava.js"></script>
<link href="sh/styles/shCore.css" rel="stylesheet" type="text/css" />
<link href="sh/styles/shThemeThymeleaf.css" rel="stylesheet" type="text/css" />
</head>
<body lang="en" dir="ltr">
<div id="page">
<div id="menu">
<ul>
<li><a href="index.html" title="Home">Home</a></li>
<li><a href="features.html" title="Features">Features</a></li>
<li><a href="download.html" title="Download">Download</a></li>
<li><a href="documentation.html" title="Documentation">Documentation</a></li>
<li><a href="ecosystem.html" title="Ecosystem">Ecosystem</a></li>
<li><a href="http://forum.thymeleaf.org" title="User Forum">User Forum</a></li>
<li><a href="issuetracking.html" title="Issue Tracking">Issue Tracking</a></li>
</ul>
</div>
<div id="header">
<a href="index.html" title="Thymeleaf home"><img src="images/thymeleaflogonameverysmall.png" class="logo" alt="Thymeleaf Template Engine"/></a>
</div>
<div id="breadcrumb">
<a href="index.html">thymeleaf</a>
::
<a href="documentation.html">documentation</a>
::
articles
::
<span class="current">bringing thymeleaf and natural templates to the spring petclinic</span>
</div>
<div id="content">
<h1>Bringing Thymeleaf and Natural Templates to the Spring PetClinic</h1>
<p id="author">
<i>By Soraya Sánchez <sschz AT users.sourceforge.net></i>
</p>
<p>
<b>Note</b>: The Spring PetClinic application received a major update from SpringSource in March 2013,
and the <i>thymeleafexamples-petclinic</i> application has been updated consequently, along with
this article.
</p>
<h2>The Spring PetClinic application</h2>
<p><i>PetClinic</i> is one of the example applications created by SpringSource for
the Spring Framework. It is designed to display and manage information
related to pets and veterinarians in a pet clinic. The original SpringSource
version lives in GitHub
<a href="https://github.com/SpringSource/spring-petclinic">here</a>, and
the thymeleaf-enabled version lives also in GitHub
<a href="https://github.com/thymeleaf/thymeleafexamples-petclinic">here</a>.</p>
<img src="images/thymeleafpetclinic_home.png" alt="PetClinic home page"/>
<p><i>Pet Clinic</i> originally includes a view layer created with JSP, which we will replace using Thymeleaf:</p>
<ul>
<li>Modifications will be focused on the view layer: the JSP files will be replaced
and the application will be reconfigured. All Java code will be left untouched.</li>
<li>The original markup will be cleaned, but all the application's interface will have
to display exactly the same as the original.</li>
<li>No CSS stylesheet files will be changed. No JavaScript libraries will be added, modified or upgraded.</li>
<li>Thymeleaf template files should display OK when opened statically on a browser (<i>Natural templates</i>).</li>
</ul>
<p>
All the code of the PetClinic+Thymeleaf application can be obtained at the
<a href="documentation.html">Thymeleaf Project's Documentation</a> page. Note that the original JSP files and JSP tags
have not been removed from the source tree but rather moved to the <kbd>doc/old_viewlayer</kbd> folder at the source tree,
so that you can still access them in order to compare with the new templates.
</p>
<p>
The version of the PetClinic application used as a base is the state of its <i>master branch at Github</i> as of 17 March 2013.
</p>
<h3>The original JSP view layer</h3>
<p>The original JSP view layer has a number of problems we will try to fix when converting the view layer
to Thymeleaf:</p>
<ul>
<li>JSPs include tags from JSTL, Spring Tag Libs and other external libraries. None of these are understandable
by browsers, so there is no way for them to display the pages statically (no static
prototyping possible).</li>
<li>JSTL tags use the JSP EL (Expression Language), whereas the tags from the JSP Spring taglibs
use Spring EL. Two different expression languages are therefore mixed in the same pages.</li>
<li>The original JSP templates are not well-formed HTML documents. For example,
the <i>"ownersList"</i> page:
<ol>
<li>Does not contain a head tag, adding instead one from another JSP using a <i>JSP include</i>
(=> not understandable by browsers).</li>
<li>Header and footer contents have been replaced by JSP include tags (=> not understandable by browsers)
so the pages can't be displayed statically including their header and footer. And anyway, even if those contents
were in the page, as pages contain JSP and JSTL tags, we wouldn't be able to see a real prototype.</li>
</ol>
</li>
</ul>
<h2>Configuration</h2>
<h3>Basic project configuration</h3>
<p>
Some basic configuration steps will be needed:
</p>
<ul>
<li>The <kbd>pom.xml</kbd> file will be modified in order to add the Thymeleaf dependencies to it and remove the
JSP-related ones.</li>
<li>The <kbd>web.xml</kbd> file will be modified in order to remove JSP-related servlets and filters.</li>
</ul>
<h3>mvc-view-config.xml</h3>
<p>
Our next configuration step will be to add three required beans at the Spring beans configuration file,
<kbd>mvc-view-config.xml</kbd>:
</p>
<ul>
<li>The Thymeleaf <i>template resolver</i> that will be in charge of reading the
template files to be processed. For this application we will use a <kbd>ServletContextTemplateResolver</kbd>.</li>
<li>The Thymeleaf <i>template engine</i> instance, of class <kbd>SpringTemplateEngine</kbd>.</li>
<li>The Thymeleaf <i>view resolver</i>, a <kbd>ThymeleafViewResolver</kbd> instance implementing Spring's
<kbd>org.springframework.web.servlet.ViewResolver</kbd> interface. This bean will substitute the
original <kbd>InternalResourceViewResolver</kbd> bean which enabled JSP support in the original application.</li>
</ul>
<script type="syntaxhighlighter" class="brush:xml"><![CDATA[
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/thymeleaf/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<!-- Template cache is set to false (default is true). -->
<property name="cacheable" value="false" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="cnManager"/>
<property name="viewResolvers">
<list>
<!-- Used here for 'xml' and 'atom' views -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1"/>
</bean>
<!-- Used for Thymeleaf views -->
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="2"/>
</bean>
</list>
</property>
</bean>
]]></script>
<p>Note that, as a difference from the original application, our templates will live at the
<kbd>/WEB-INF/thymeleaf</kbd> folder instead of the original <kbd>/WEB-INF/jsp</kbd>.</p>
<h2>From JSP to Thymeleaf</h2>
<p>
PetClinic includes more than 10 JSP templates, and we will rewrite all of them using Thymeleaf.
However, for the sake of brevity, we will focus on <kbd>owners/ownerslist.jsp</kbd>, which we will
convert into <kbd>owners/ownersList.html</kbd>.
</p>
<p>
Remember you can see all the templates at the source code, downloadable from
<a href="documentation.html">the documentation page</a>, and also that you can review the original
JSP files at the <kbd>doc/old_viewlayer</kbd> folder.
</p>
<p>
The <i>owners/ownersList</i> page looks like this:
</p>
<img src="images/thymeleafpetclinic_owners.png" alt="Owners page"/>
<p>In order to convert this page to Thymeleaf, we will:</p>
<ul>
<li>Rename <kbd>ownersList.jsp</kbd> to <kbd>ownersList.html</kbd>.</li>
<li>Remove all <kbd><%@ taglib %></kbd> directives as we do not need any JSP tag libraries</li>
<li>Replace the <kbd>jsp:include</kbd> tags which add head, header and footer to the page with tags containing
the thymeleaf attributes <kbd>th:substituteby</kbd> or <kbd>th:include</kbd>. Those page fragments
have been kept in the <kbd>fragments</kbd> folder and converted to thymeleaf as well</li>
</ul>
<script type="syntaxhighlighter" class="brush:html"><![CDATA[
<!-- ownersList.jsp -->
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="datatables" uri="http://github.com/dandelion/datatables" %>
<html lang="en">
<jsp:include page="../fragments/headTag.jsp"/>
<body>
<div class="container">
<jsp:include page="../fragments/bodyHeader.jsp"/>
<!-- ... -->
<jsp:include page="../fragments/footer.jsp"/>
</div>
</body>
</html>
]]></script>
<script type="syntaxhighlighter" class="brush:html"><![CDATA[
<!-- ownersList.html -->
<!DOCTYPE html>
<html lang="en">
<head th:substituteby="fragments/headTag :: headTag">
<!-- ============================================================================ -->
<!-- This <head> is only used for static prototyping purposes (natural templates) -->
<!-- and is therefore entirely optionl, as this markup fragment will be included -->
<!-- from "fragments.html" at runtime. -->
<!-- ============================================================================ -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>PetClinic :: a Spring Framework demonstration</title>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap.min.css"
th:href="@{/webjars/bootstrap/2.3.0/css/bootstrap.min.css}" rel="stylesheet" />
<link href="../../../resources/css/petclinic.css"
th:href="@{/resources/css/petclinic.css}" rel="stylesheet" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"
th:src="@{/webjars/jquery/1.9.0/jquery.js}"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"
th:src="@{/webjars/jquery-ui/1.9.2/js/jquery-ui-1.9.2.custom.js}"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/smoothness/jquery-ui.css"
th:href="@{/webjars/jquery-ui/1.9.2/css/smoothness/jquery-ui-1.9.2.custom.css}"
rel="stylesheet" />
</head>
<body>
<div class="container">
<div th:include="fragments/bodyHeader" th:remove="tag">
<!-- =========================================================================== -->
<!-- This div is only used for static prototyping purposes (natural templates) -->
<!-- and is therefore entirely optionl, as this markup fragment will be included -->
<!-- from "fragments.html" at runtime. -->
<!-- =========================================================================== -->
<img th:src="@{/resources/images/banner-graphic.png}"
src="../../../resources/images/banner-graphic.png"/>
<div class="navbar" style="width: 601px;">
<div class="navbar-inner">
<ul class="nav">
<li style="width: 100px;"><a
href="../welcome.html" th:href="@{/}"><i
class="icon-home"></i>Home</a></li>
<li style="width: 130px;"><a
href="../owners/findOwners.html" th:href="@{/owners/find.html}"><i
class="icon-search"></i>Find owners</a></li>
<li style="width: 140px;"><a
href="../vets/vetList.html" th:href="@{/vets.html}"><i
class="icon-th-list"></i>Veterinarians</a></li>
<li style="width: 90px;"><a
href="../exception.html" th:href="@{/oups.html}"
title="trigger a RuntimeException to see how it is handled"><i
class="icon-warning-sign"></i>Error</a></li>
<li style="width: 80px;"><a
href="#" title="not available yet. Work in progress!!"><i
class=" icon-question-sign"></i>Help</a></li>
</ul>
</div>
</div>
</div>
<!-- ... -->
<table th:substituteby="fragments/footer :: footer" class="footer">
<!-- =========================================================================== -->
<!-- This table section is only used for static prototyping purposes (natural -->
<!-- templates) and is therefore entirely optionl, as this markup fragment will -->
<!-- be included from "fragments.html" at runtime. -->
<!-- =========================================================================== -->
<tr>
<td></td>
<td align="right">
<img src="../../../resources/images/springsource-logo.png"
th:src="@{/resources/images/springsource-logo.png}"
alt="Sponsored by SpringSource" />
</td>
</tr>
</table>
</div>
</body>
</html>
]]></script>
<p>
Note how our <kbd>ownersList.html</kbd> contains more code at its head, header and footer
sections than the original JSP file. Doing it this way is merely optional, and its only aim is to allow the
<kbd>ownersList.html</kbd> Thymeleaf-enabled template to
display statically as a prototype (something nearly impossible with JSP).
</p>
<p>
<i>Is this additional code worth it?</i> If you need or want to use design prototypes, indeed! You will see clearly how much
a difference this is at the last section of this article. And anyway... remember this prototyping code is optional!
</p>
<ul>
<li>
Change the page body. The original code looks like this:
</li>
</ul>
<script type="syntaxhighlighter" class="brush:html"><![CDATA[
<!-- ownersList.jsp -->
<datatables:table id="owners" data="${selections}" cdn="true" row="owner" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" export="pdf">
<datatables:column title="Name" cssStyle="width: 150px;" display="html">
<spring:url value="owners/{ownerId}.html" var="ownerUrl">
<spring:param name="ownerId" value="${owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(ownerUrl)}"><c:out value="${owner.firstName} ${owner.lastName}"/></a>
</datatables:column>
<datatables:column title="Name" display="pdf">
<c:out value="${owner.firstName} ${owner.lastName}"/>
</datatables:column>
<datatables:column title="Address" property="address" cssStyle="width: 200px;"/>
<datatables:column title="City" property="city"/>
<datatables:column title="Telephone" property="telephone"/>
<datatables:column title="Pets" cssStyle="width: 100px;">
<c:forEach var="pet" items="${owner.pets}">
<c:out value="${pet.name}"/>
</c:forEach>
</datatables:column>
<datatables:export type="pdf" cssClass="btn btn-small" />
</datatables:table>
]]></script>
<p>
Which we will replace with:
</p>
<script type="syntaxhighlighter" class="brush:html"><![CDATA[
<!-- ownersList.html -->
<h2>Owners</h2>
<table class="table table-striped">
<thead>
<tr>
<th style="width: 150px;">Name</th>
<th style="width: 200px;">Address</th>
<th>City</th>
<th>Telephone</th>
<th style="width: 100px;">Pets</th>
</tr>
</thead>
<tbody>
<tr th:each="owner : ${selections}">
<td>
<a href="ownerDetails.html"
th:href="@{|/owners/${owner.id}|}"
th:text="|${owner.firstName} ${owner.lastName}|">Mary Smith</a>
</td>
<td th:text="${owner.address}">45, Oxford Street</td>
<td th:text="${owner.city}">Cambridge</td>
<td th:text="${owner.telephone}">555-555-555</td>
<td>
<span th:each="pet : ${owner.pets}" th:text="${pet.name}" th:remove="tag">
Rob
</span>
</td>
</tr>
</tbody>
</table>
]]></script>
<ul>
<li>
In the code above you can see how we used HTML code instead of a collection
of JSP tags from an external library. Not only this makes our code much clearer and more readable,
but also more standard and <i>understandable by browsers</i>, which will allow us to use this template
as a <i>static prototype</i>. Again, we will see the advantages of this in the next section.
</li>
</ul>
<h2>And what about the <i>Natural Templates</i> thing?</h2>
<p>
Before we started this migration, we set a goal that our new Thymeleaf templates would
be able to display correctly when open statically in a browser (without starting the application
server) thanks to the <i>Natural Templating</i> capabilities of Thymeleaf.
</p>
<p>
Well, let's have a look at how the original <kbd>owners/ownersList.jsp</kbd> template looks like when seen
statically:
</p>
<img src="images/thymeleafpetclinic_ownerslist_jsp_static.png" alt="Owners list (JSP), statically opened"/>
<p>
...and now let's have a look at our new Thymeleaf-powered <kbd>owners/ownersList.html</kbd>:
</p>
<img src="images/thymeleafpetclinic_ownerslist_thymeleaf_static.png" alt="Owners list (thymeleaf), statically opened"/>
<p>
There we are. Data is not valid, because it is a prototype. But it looks good!
</p>
</div>
<div id="footer">
<div id="googleplus">
<div id="plusone-div" class="plusone"></div>
<script type="text/javascript">
gapi.plusone.render('plusone-div',{"size": "small", "count": "true", "href": "http://www.thymeleaf.org"});
</script>
</div>
<div id="twitter">
<a href="http://twitter.com/thymeleaf" class="twitter-follow-button" data-show-count="false">Follow @thymeleaf</a>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
</div>
<div id="copy">
Copyright © The <a href="team.html">THYMELEAF Team</a>. See <a href="documentation.html">applicable licenses</a>.
</div>
</div>
</div>
<script type="text/javascript">
SyntaxHighlighter.all();
</script>
</body>
</html>