π·Thymeleaf - Standard URL Syntax
β 1.Absolute URLs (μ λκ²½λ‘)
- μ λ URLμ μ¬μ©νλ©΄ λ€λ₯Έ μλ²μ λν λ§ν¬λ₯Ό λ§λ€ μ μμ΅λλ€. νλ‘ν μ½ μ΄λ¦ ( http://λλ https://) μ μ§μ νμ¬ μμν©λλ€.
- <a th:href="@{http://www.thymeleaf/documentation.html}">
- κ·Έλ€μ μ ν μμ λμ§ μμ΅λλ€ ( μλ²μ URL μ¬ μμ± νν°κ° ꡬμ±λμ΄ μκ³ HttpServletResponse.encodeUrl(...)λ°©λ² μμ μμ μ μν νμ§ μλ ν )
- <a href="http://www.thymeleaf/documentation.html">
β 2. Context-relative URLs (μλκ²½λ‘)
- Tκ°μ₯ λ§μ΄ μ¬μ©λλ URL μ νμ λ¬Έλ§₯μ λ°λ₯Έ URL μ
λλ€. μ΄λ€μ μΉ μμ© νλ‘κ·Έλ¨ λ£¨νΈκ° μλ²μ μ€μΉλλ©΄ μλμ μΈ URLμ
λλ€. μ°λ¦¬κ° λ°°ν¬νλ κ²½μ° μλ₯Ό λ€μ΄, myapp.warTomcat μλ²μ νμΌμ, μ°λ¦¬μ μμ© νλ‘κ·Έλ¨μ μλ§λ μ‘μΈμ€ ν μ http://localhost:8080/myappλ° myappλ κ² μ»¨ν
μ€νΈ μ΄λ¦ .
- λ¬Έλ§₯ μλ URLμ λ€μμΌλ‘ μμν©λλ€ /.
- <a th:href="@{/order/list}">
- μ μ±μ΄ μ€μΉλ http://localhost:8080/myappκ²½μ°μ΄ URLμ λ€μμ μΆλ ₯ν©λλ€.
- <a href="/myapp/order/list">
β 3. Server-relative URLs (μλ² μλκ²½λ‘)
- μλ² μλ URLμ URLμ΄ μ ν리μΌμ΄μ
컨ν
μ€νΈ λ΄μ 리μμ€μ λ§ν¬λλ κ²μ μνμ§ μλλ€λ μ μ μ μΈνκ³ μ»¨ν
μ€νΈ μλ URLκ³Ό λ§€μ° μ μ¬ νλ―λ‘ λμΌν μλ²μ λ€λ₯Έ 컨ν
μ€νΈμ λ§ν¬ ν μ μμ΅λλ€.
- <a th:href="@{~/billing-app/showDetails.htm}">
- νμ¬ μ ν리μΌμ΄μ
μ 컨ν
μ€νΈλ 무μλλ―λ‘ μ ν리μΌμ΄μ
μ΄μ λ°°ν¬λμμ§λ§ http://localhost:8080/myappμ΄ URLμ λ€μμ μΆλ ₯ν©λλ€.
- <a href="/billing-app/showDetails.htm">
β 4. Protocol-relative URLs (νλ‘ν μ½ κΈ°μ€κ²½λ‘)
- νλ‘ν μ½ κΈ°μ€ URLμ μ€μ λ‘ νμ¬ νμ΄μ§λ₯Ό νμνλ λ° μ¬μ©λλ νλ‘ν μ½ (HTTP, HTTPS)μ μ μ§νλ μ λ URLμ
λλ€. μΌλ°μ μΌλ‘ μ€νμΌ, μ€ν¬λ¦½νΈ λ±κ³Ό κ°μ μΈλΆ 리μμ€λ₯Ό ν¬ν¨νλ λ° μ¬μ©λ©λλ€.
- <script th:src="@{//scriptserver.example.net/myscript.js}">...</script>
- β¦ μμ λμ§ μμ μνλ‘ λ λλ§λ©λλ€ ( URL μ¬ μμ± μ μΈ ).
- <script src="//scriptserver.example.net/myscript.js">...</script>
β 5. Adding parameters (λ§€κ°λ³μ μΆκ°)
- @{...}ννμμΌλ‘ μμ± ν URLμ λ§€κ° λ³μλ₯Ό μ΄λ»κ² μΆκ° ν©λκΉ? λ¨μν:
- <a th:href="@{/order/details(id=3)}">
- λ€μκ³Ό κ°μ΄ μΆλ ₯λ©λλ€.
- <a href="/order/details?id=3">
- μΌνλ‘ κ΅¬λΆνμ¬ μ¬λ¬ λ§€κ° λ³μλ₯Ό μΆκ° ν μ μμ΅λλ€.
- <a th:href="@{/order/details(id=3,action='show_all')}">
- λ€μκ³Ό κ°μ΄ μΆλ ₯λ©λλ€.
- <!-- Note ampersands (&) should be HTML-escaped in tag attributes... -->
<a href="/order/details?id=3&action=show_all">
- μΌλ° λ§€κ° λ³μ μ μ μ¬ νμ§λ§ URL κ²½λ‘ λ΄μ μ리 νμμλ₯Ό μ§μ νμ¬ κ²½λ‘ λ³μ νμμΌλ‘ λ§€κ° λ³μλ₯Ό ν¬ν¨ ν μλ μμ΅λλ€ .
- <a th:href="@{/order/{id}/details(id=3,action='show_all')}">
- λ€μκ³Ό κ°μ΄ μΆλ ₯λ©λλ€.
-<a href="/order/3/details?action=show_all">
β 6. URL fragment identifiers
- ννΈ μλ³μλ λ§€κ° λ³μλ₯Ό ν¬ν¨νκ±°λ ν¬ν¨νμ§ μκ³ URLμ ν¬ν¨λ μ μμ΅λλ€. νμ URLλ² μ΄μ€μ ν¬ν¨λλ―λ‘ λ€μμ μνν©λλ€.
- <a th:href="@{/home#all_info(action='show')}">
- β¦ λ€μκ³Ό κ°μ΄ μΆλ ₯λ©λλ€.
- <a href="/home?action=show#all_info">
β 7. URL rewriting
- Thymeleafλ₯Ό μ¬μ©νλ©΄ μ ν리μΌμ΄μ
μμ URL μ¬ μμ± νν° λ₯Ό κ΅¬μ± ν μ μμΌλ©° Thymeleaf ν
νλ¦Ώμμ μμ± λ λͺ¨λ URLμ λν΄ Servlet API response.encodeURL(...)μ javax.servlet.http.HttpServletResponseν΄λμ€ μμ λ©μλλ₯Ό νΈμΆνμ¬μ΄λ₯Ό μν ν©λλ€.
- μ΄κ²μ Java μΉ μ ν리μΌμ΄μ
μμ URL μ¬ μμ± μμ
μ μ§μνλ νμ€ λ°©λ²μ΄λ©° URLμ΄ λ€μμ μν ν μ μλλ‘ν©λλ€.
- μ¬μ©μκ° μΏ ν€λ₯Ό νμ±ννλμ§ μ¬λΆλ₯Ό μλμΌλ‘ κ°μ§νκ³ ;jsessionid=...κ·Έλ μ§ μμ κ²½μ° URLμ μ‘°κ°μ μΆκ°νκ±°λ 첫 λ²μ§Έ μμ²μ΄κ³ μΏ ν€ κ΅¬μ±μ μμ§ μ μμλ κ²½μ°.
- νμν κ²½μ° URLμ νλ‘μ ꡬμ±μ μλμΌλ‘ μ μ©ν©λλ€.
- μ¬λ¬ μλ²μ λ°°ν¬ λ μ½ν
μΈ μ μ°κ²°νλ €λ©΄ μλ‘ λ€λ₯Έ CDN (Content Delivery Network) μ€μ μ μ¬μ©ν©λλ€ (ꡬμ±λ κ²½μ°).
- URL μ¬ μμ±μμν λ§€μ° μΌλ°μ μ΄λ©° κΆμ₯λλ κΈ°μ μ URLRewriteFilter μ
λλ€.
β 8. Only for th:hrefβs?
- URL @{...}ννμμ΄ th:hrefμμ± μλ§ μ¬μ©λλ€κ³ μκ°νμ§ λ§μμμ€ . μ¬μ€ λ³μ νν ( )μ΄λ λ©μμ§ μΈλΆν / κ΅μ ν νν ( ) μ²λΌ μ΄λμμλ μ¬μ©ν μ μμ΅λλ€ .${...}#{...}
- μλ₯Ό λ€μ΄ μμμ μ¬μ©ν μ μμ΅λλ€.
- <form th:action="@{/order/processOrder}">
- β¦ λλ λ€λ₯Έ ννμ μΌλΆλ‘. λ€μμ μΈλΆν / κ΅μ ν λ¬Έμμ΄μ λ§€κ° λ³μμ
λλ€.
- <p th:text="#{orders.explanation('3', @{/order/details(id=3,action='show_all')})}">
β 9. Using expressions in URLs
- λ€μκ³Ό κ°μ URL ννμμ μμ±ν΄μΌνλ κ²½μ° :
- <a th:href="@{/order/details(id=3,action='show_all')}">
- ...νμ§λ§ μ΄λ μͺ½ 3λ 'show_all'μ°λ¦¬λ μ€ν μκ°μ μμ μ κ°μΉλ₯Ό μκ³ μκΈ° λλ¬Έμ, 리ν°λ΄ λ μ μμκΉ?
- λ¬Έμ μμ΄μ! λͺ¨λ URL λ§€κ° λ³μ κ°μ μ¬μ€ ννμμ΄λ―λ‘ i18n, 쑰건문β¦μ ν¬ν¨ν λ€λ₯Έ ννμμΌλ‘ 리ν°λ΄μ μ½κ² λ체 ν μ μμ΅λλ€.
- <a th:href="@{/order/details(id=${order.id},action=(${user.admin} ? 'show_all' : 'show_public'))}">
- μΆκ° κΈ°λ₯ : λ€μκ³Ό κ°μ URL ννμ :
- <a th:href="@{/order/details(id=${order.id})}">
- β¦ μ¬μ€ λ€μμμν μ§λ¦κΈΈμ
λλ€.
- <a th:href="@{'/order/details'(id=${order.id})}">
- μ¦, URLλ² μ΄μ€ μ체λ₯Ό ννμ (μ : λ³μ ννμ)μΌλ‘ μ§μ ν μ μμ΅λλ€.
- <a th:href="@{${detailsURL}(id=${order.id})}">
- β¦ λλ μΈλΆν λ / κ΅μ ν λ ν
μ€νΈ :
- <a th:href="@{#{orders.details.localized_url}(id=${order.id})}">
- ⦠쑰건문μ ν¬ν¨νμ¬ λ³΅μ‘ν ννμλ μ¬μ©ν μ μμ΅λλ€. μλ₯Ό λ€λ©΄ λ€μκ³Ό κ°μ΅λλ€.
- <a th:href="@{(${user.admin}? '/admin/home' : ${user.homeUrl})(id=${order.id})}">
- μ²μκΈ°λ₯Ό μνμλκΉ? μ¬μ© th:with:
- <a th:with="baseUrl=(${user.admin}? '/admin/home' : ${user.homeUrl})"
th:href="@{${baseUrl}(id=${order.id})}">
- β¦λλβ¦
- <div th:with="baseUrl=(${user.admin}? '/admin/home' : ${user.homeUrl})">
...
<a th:href="@{${baseUrl}(id=${order.id})}">...</a>
...
</div>