javascript
015_SpringBoot视图层技术thymeleaf-URL表达式
1. url表達式: th:href和th:src。
2. url表達式基本語法: @{}。
3. 絕對路徑: <a th:href="@{http://www.baidu.com}">Thymeleaf絕對路徑-百度</a>。
4. 相對路徑
4.1. 相對于當前項目的根, 相對于項目的上下文的相對路徑: <a th:href="@{/show}">相對路徑</a>。
4.2. 相對于服務器(Tomcat)路徑的根: <a th:href="@{~/Project/resourcename}">相對于服務器的根</a>。
4.3. 在url中實現參數傳遞: <a th:href="@{/page(id=1,name=zhangsan)}">相對路徑-傳參</a>。
4.4. 在url中通過restful風格進行參數傳遞: <a th:href="@{/article/details/{articleId}(articleId=1001)}">相對路徑-restful</a>。
4. Thymeleaf?URL表達式案例
1. 使用maven構建SpringBoot的名叫spring-boot-view-thymeleaf-url項目
2. pom.xml?
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjbs</groupId><artifactId>spring-boot-view-thymeleaf-url</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version><!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html標簽不規范報錯 --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的啟動器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies> </project>3. 在src/main/resources/templates下新建page.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>Thymeleaf-URL表達式</title></head><body><a th:href="@{http://www.baidu.com}">Thymeleaf絕對路徑-百度</a><br/><a href="http://www.baidu.com">html絕對路徑-百度</a><hr/><a th:href="@{/show}">相對路徑</a><hr/><a th:href="@{~/images/java.jpg}">相對于服務器的根</a><hr/><a th:href="@{/page(id=1,name=zhangsan)}">相對路徑-傳參</a><hr/><a th:href="@{/article/details/{articleId}(articleId=1001)}">相對路徑-restful</a></body> </html>4. 在src/main/resources/templates/article/details下新建articledetails.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>文章詳情</title></head><body><h1><span th:text="${title}"></span></h1></body> </html>5. 新建UserController.java
package com.bjbs.controller;import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;@Controller public class UserController {@RequestMapping("/show")@ResponseBodypublic Map<String, Object> showInfo(Model model) {Map<String, Object> map = new HashMap<>();map.put("msg", "HelloWorld");return map;}// PathVariable路徑變量@RequestMapping("/{page}")public String pathVariable(@PathVariable String page, Integer id, String name) {System.out.println("pathVariable page = " + page + ", id = " + id + ", name = " + name);return page;}@RequestMapping("/article/details/{articleId}")public String restful(@PathVariable Integer articleId, Model model) {System.out.println("restful articleId = " + articleId);// 數據庫查詢articleId的文章model.addAttribute("title", "第" + articleId + "篇文章的標題");return "article/details/articledetails";} }6. 新建App.java
package com.bjbs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot啟動類*/ @SpringBootApplication public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }7. 啟動項目, 并使用瀏覽器訪問
8. 訪問Thymeleaf絕對路徑-百度?
9. 訪問相對路徑?
10. 訪問相對于服務器的根?
11. 訪問相對路徑-傳參?
12. 訪問相對路徑-restful?
總結
以上是生活随笔為你收集整理的015_SpringBoot视图层技术thymeleaf-URL表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 014_SpringBoot视图层技术t
- 下一篇: 016_SpringBoot整合MyBa