forked from thymeleaf/thymeleaf.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standardurlsyntax.html
executable file
·419 lines (310 loc) · 13.7 KB
/
standardurlsyntax.html
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
<!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>Standard URL syntax - 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"></script>
<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">standard url syntax</span>
</div>
<div id="content">
<h1>Standard URL Syntax</h1>
<p>
The Thymeleaf standard dialects —called <i>Standard</i> and <i>SpringStandard</i>— offer a way to easily create
URLs in your web applications so that they include any required <i>URL preparation</i> artifacts. This is done by means of the
so-called <i>“at” syntax</i> of the <i>standard expressions</i>: <kbd>@{...}</kbd>
</p>
<h2>Absolute URLs</h2>
<p>
Absolute URLs allow you to create links to other servers. They start by specifying a protocol
name (<kbd>http://</kbd> or <kbd>https://</kbd>)
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{http://www.thymeleaf/documentation.html}">
]]></script>
<p>
They are not modified at all (unless you have an <em>URL Rewriting</em> filter
configured at your server):
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a href="http://www.thymeleaf/documentation.html">
]]></script>
<h2>Context-relative URLs</h2>
<p>
The most used type of URLs are <i>context-relative</i> ones. These are URLs which are supposed to be
relative to the web application root once it is installed on the server. For example, if we deploy
a <kbd>myapp.war</kbd> file into a Tomcat server, our application will probably be accessible as
<kbd>http://localhost:8080/myapp</kbd>, and <kbd>myapp</kbd> will be the <i>context name</i>.
</p>
<p>
Context-relative URLs start with <kbd>/</kbd>:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{/order/list}">
]]></script>
<p>
If our app is installed at <kbd>http://localhost:8080/myapp</kbd>, this URL will output:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a href="/myapp/order/list">
]]></script>
<h2>Server-relative URLs</h2>
<p>
<i>Server-relative</i> URLs are very similar to <i>context-relative</i> URLs, except they do not assume
you want your URL to be linking to a resource inside your application's context, and therefore
allow you to link to a different context in the same server:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{~/billing-app/showDetails.htm}">
]]></script>
<p>
The current application's context will be ignored, therefore although our application is deployed at
<kbd>http://localhost:8080/myapp</kbd>, this URL will output:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a href="/billing-app/showDetails.htm">
]]></script>
<h2>Protocol-relative URLs</h2>
<p>
<i>Protocol-relative</i> URLs are in fact absolute URLs which will keep the protocol
(HTTP, HTTPS) being used for displaying the current page. They are typically used for including
external resources like styles, scripts, etc.:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<script th:src="@{//scriptserver.example.net/myscript.js}">...</script>
]]></script>
<p>
...which will render unmodified, as:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<script src="//scriptserver.example.net/myscript.js">...</script>
]]></script>
<h2>Adding parameters</h2>
<p>
How do we add parameters to the URLs we create with <kbd>@{...}</kbd>
expressions? Simple:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{/order/details(id=3)}">
]]></script>
<p>
Which would output as:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a href="/order/details?id=3">
]]></script>
<p>
You can add several parameters, separating them with commas:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{/order/details(id=3,action='show_all')}">
]]></script>
<p>
Which would output as:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a href="/order/details?id=3&action=show_all">
]]></script>
<p>
You can also include parameters in the form of <em>path variables</em> similarly
to <em>normal</em> parameters but specifying a placeholder inside your URL's path:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{/order/{id}/details(id=3,action='show_all')}">
]]></script>
<p>
Which would output as:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a href="/order/3/details?action=show_all">
]]></script>
<h2>URL fragment identifiers</h2>
<p>
Fragment identifiers can be included in URLs, both with and without parameters. They will always
be included at the URL base, so that:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{/home#all_info(action='show')}">
]]></script>
<p>
...would output as:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a href="/home?action=show#all_info">
]]></script>
<h2>URL Rewriting</h2>
<p>
Thymeleaf allows you to configure <em>URL rewriting filters</em> in your applications,
and it does so by calling the <kbd>response.encodeURL(...)</kbd> method in the
<kbd>javax.servlet.http.HttpServletResponse</kbd> class of the Servlet API for every URL
generated from a thymeleaf template.
</p>
<p>
This is the standard way of supporting URL rewriting operations in Java web applications,
and allows URLs to:
</p>
<ul>
<li>
Automatically detect whether the user has cookies enabled or not, and
add the <kbd>;jsessionid=...</kbd> fragment to the URL if not —or if it is the
first request and cookie configuration is still unknown.
</li>
<li>
Automaticallty apply proxy configuration to URLs when needed.
</li>
<li>
Make use (if configured so) of different CDN (Content Delivery Network) setups,
in order to link to content distributed among several servers.
</li>
</ul>
<p>
A very common (and recommended) technology for URL rewriting is
<a href="http://tuckey.org/urlrewrite/" target="_blank">URLRewriteFilter</a>.
</p>
<h2>Only for th:href's?</h2>
<p>
Do not think URL <kbd>@{...}</kbd> expressions are only used in <kbd>th:href</kbd> attributes. They can,
in fact, be used <i>anywhere</i> just like variable expressions (<kbd>${...}</kbd>) or message externalization /
internationalization ones (<kbd>#{...}</kbd>).
</p>
<p>
For example, you could use them in forms...
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<form th:action="@{/order/processOrder}">
]]></script>
<p>
...or as a part of other expression. Here as a parameter of an externalized/internationalized string:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<p th:text="#{orders.explanation('3', @{/order/details(id=3,action='show_all')})}">
]]></script>
<h2>Using expressions in URLs</h2>
<p>
What if we needed to write an URL expression like this:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{/order/details(id=3,action='show_all')}">
]]></script>
<p>
...but neither <kbd>3</kbd> nor <kbd>'show_all'</kbd> could be literals, because we only know their
value at run time?
</p>
<p>
No problem! Every URL parameter value is in fact an expression, so you can easily substitute
your literals with any other expressions, including i18n, conditionals...:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{/order/details(id=${order.id},action=(${user.admin} ? 'show_all' : 'show_public'))}">
]]></script>
<p>
What's more: an URL expression like:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{/order/details(id=${order.id})}">
]]></script>
<p>
...is in fact a shortcut for:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{'/order/details'(id=${order.id})}">
]]></script>
<p>
Which means that the URL base itself can be specified as an expression, for example
a variable expression:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{${detailsURL}(id=${order.id})}">
]]></script>
<p>
...or an externalized/internationalized text:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{#{orders.details.localized_url}(id=${order.id})}">
]]></script>
<p>
...even complex expressions can be used, including conditionals, for example:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:href="@{(${user.admin}? '/admin/home' : ${user.homeUrl})(id=${order.id})}">
]]></script>
<p>
Want it cleaner? Use <kbd>th:with</kbd>:
</p>
<script type="syntaxhighlighter" class="brush:html;gutter:false"><![CDATA[
<a th:with="baseUrl=(${user.admin}? '/admin/home' : ${user.homeUrl})"
th:href="@{${baseUrl}(id=${order.id})}">
]]></script>
<p>
...or...
</p>
<script type="syntaxhighlighter" class="brush:html"><![CDATA[
<div th:with="baseUrl=(${user.admin}? '/admin/home' : ${user.homeUrl})">
...
<a th:href="@{${baseUrl}(id=${order.id})}">...</a>
...
</div>
]]></script>
</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>