SpringBoot微服務框架講解
、
**01、thymeleaf 頁面輸出**
在之前模板渲染的課程中有講解過 th:text將文本內容進行輸出,但是
thymeleaf中不止這樣一種輸出方式還有一種 th:utext
的形式輸出。
建立一個 springboot項目;項目名稱爲 xmedu-thymeleaf;
默認項目啓動名稱修改爲 StartSpringBootApplication.java
在項目中 jar如需要的 thymeleaf的 jar文件包
範例:thymeleaf的 jar
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 在不同的包中建立控制器代碼,不能掃描到的時候需要進行配置包掃 描“@ComponentScan(basePackages = "cn.xmkeshe.controller")”,在啓動 程序類上配置,如果是在同一個包中就需要配置。 範例:建立控制器方法
@RequestMapping("/admin/show") public String show(Model model) { model.addAttribute("url", "www.xmkeshe.cn"); model.addAttribute("msg", "恭喜你已經登錄成功!"); return "admin/show"; }
範例:編寫 show.html頁面
<p th:text="${url}"></p> <p th:text="${msg}"></p> 上面的信息可以正常輸出,非常完美。執行路徑:
http://localhost/admin/show 範例:修改後臺代碼
@RequestMapping("/admin/show") public String show(Model model) { model.addAttribute("url", "<a href='http://www.xmkeshe.cn'>www.xmkeshe.cn</a>"); model.addAttribute("msg", "恭喜你已經登錄成功!"); return "admin/show"; }
範例:修改頁面
<p th:text="${url}"></p> <p th:text="${msg}"></p> <hr/> <p th:utext="${url}"></p> 觀察 th:utext的輸出結果與 th:text輸出結果;