第三章:SpringBoot配置深刻-4. 模版渲染

在以前所見到的信息顯示發現都是以 Rest 風格進行顯示,可是很明顯在實際的開發之中,全部數據的顯示最終都應該交由頁 面完成,可是這個頁面並非*.jsp 頁面,而是普通的*.html 頁面,並且最爲重要的是,此處所使用的渲染的頁面採用的是模版方式 的顯示,而在 Java 開發行業,對於前臺的顯示模版常見的一共有三類技術:FreeMarker、Velocity、thymeleaf(推薦使用),因而下 面就利用 thymeleaf 實現一個簡單的模版渲染操做。css

一、 若是要想在項目之中去使用 thymeleaf 模版,那麼應該首先進行相關支持依賴庫的導入,修改 pom.xml 配置文件:html

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

完整pomjava

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.mldn</groupId>
		<artifactId>microboot</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>microboot-advance</artifactId>
	<name>microboot-advance</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<!-- 添加模板 -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>

	</dependencies>
</project>

二、 本次的開發依然要經過一個控制層跳轉到頁面之中進行信息顯示。在 SpringMVC 的時代使用的是 ModelAndView 傳遞,而現 在在 SpringBoot 裏面若是要傳遞直接在方法中定義一個 Model 參數便可。web

package cn.mldn.microboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.mldn.microboot.util.controller.AbstractBaseController;

@Controller
public class MessageController extends AbstractBaseController {
    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String show(String mid, Model model) {
        model.addAttribute("url", "www.study.cn"); // request屬性傳遞包裝
        model.addAttribute("mid", mid); // request屬性傳遞包裝
        return "message/message_show"; // 此處只返回一個路徑, 該路徑沒有設置後綴,後綴默認是*.html
    }
}

三、 如今的控制器之中使用的是「@Controller」註解,因此此時執行該控制器的方法後會進行跳轉處理。若是如今要進行跳轉 頁面的定義,有嚴格要求:在CLASSPATH路徑下(src/main/resources、src/main/view)必須創建有一個templates的目錄,在這個目錄裏面保存有thymeleaf的全部相關頁面,這些頁面能夠按照文件目錄保存;spring

創建一個源代碼目錄:src/main/view;apache

而後項目上右鍵properties  進行設置提高爲源代碼目錄app

四、 編寫 message_show.html 頁面(重要提示:該頁面編寫的時候全部的元素必定要完結jsp

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<p th:text="'官方網站:' + ${url}"/>
<p th:text="'用戶名:' + ${mid}"/>
</body>
</html>

五、 運行服務,然後輸入訪問路徑:http://localhost/show?mid=lgs;maven

 六、 若是如今咱們所定義的要訪問的頁面不是經過控制器跳轉的怎麼辦?那麼爲了解決這樣的問題,能夠考慮在 thymeleaf 所在的 父路徑之中「src/main/view」創建一個 static 的子目錄,該目錄保存的是全部靜態頁面;spring-boot

在之後的實際開發之中,像 js、css、images 等信息文件都要求放在 static 目錄裏面。

 七、 thymeleaf 默認的訪問的頁面路徑的後綴爲*.html,那麼也能夠經過修改 application.yml 配置文件進行變動:

spring:     # 表示該配置直接爲Spring容器負責處理
  thymeleaf:
    suffix: .htm
  messages: # 表示進行資源配置
    basename: i18n/Messages,i18n/Pages  # 資源文件的名稱
server:
  port: 80      # 此處設置的服務的訪問端口配置

 這個時候你須要修改「src/main/view/templates/message」下的 message_show.html 頁面爲 message_show.htm 才能夠正常訪問。

相關文章
相關標籤/搜索