玩轉(zhuǎn)spring boot MVC應(yīng)用(2)
如何快速搭建一個(gè)MCV程序?
參照spring官方例子:https://spring.io/guides/gs/serving-web-content/
一、spring mvc結(jié)合thymeleaf模板
創(chuàng)建maven project后,修改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.github.carter659</groupId> <artifactId>spring02</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <name>spring02</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
添加“MainController.java”這個(gè)控制器的類(lèi)文件:
package com.github.carter659.spring02; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class MainController { @GetMapping("/") public String index(Model model) { model.addAttribute("name", "劉冬"); return "index"; } }
修改App.java文件
package com.github.carter659.spring02; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
然后在項(xiàng)目中右鍵進(jìn)入java build path
添加文件夾“And Folder”
在main目錄下添加“resources”文件夾
修改"resources"的“Excluded”:
輸入“**”
在src/main/resources下創(chuàng)建“templates”文件夾,并新建一個(gè)html文件“index.html”
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>MVC</title> </head> <body> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html>
輸入http://localhost:8080 檢測(cè)是否運(yùn)行成功:
以上是使用thymeleaf模板做的動(dòng)態(tài)頁(yè)面,那么,如何在MVC應(yīng)用中使用靜態(tài)資源呢?
二、靜態(tài)資源
在src\main\resources下新建“static”文件夾
并在其文件夾中復(fù)制進(jìn)一張圖片文件
修改之前的“index.html”文件,增加img標(biāo)簽
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>MVC</title> </head> <body> <img alt="ae86" src="img.png" /> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html>
這時(shí),立刻出現(xiàn)一個(gè)現(xiàn)象:
我們發(fā)現(xiàn)程序會(huì)自動(dòng)熱加載,這是因?yàn)樵趍aven中依賴(lài)了“devtools”
最后,刷新網(wǎng)頁(yè),測(cè)試靜態(tài)資源是否載入
PS:spring boot主推的是thymeleaf模板,而其語(yǔ)言用的是xml,個(gè)人認(rèn)為不是非常方便。
代碼下載:https://github.com/carter659/spring-boot-02.git
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
activiti實(shí)現(xiàn)員工請(qǐng)假流程解析
這篇文章主要介紹了activiti實(shí)現(xiàn)員工請(qǐng)假流程解析,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Java服務(wù)限流算法的6種實(shí)現(xiàn)
服務(wù)限流是指通過(guò)控制請(qǐng)求的速率或次數(shù)來(lái)達(dá)到保護(hù)服務(wù)的目的,本文主要介紹了Java服務(wù)限流算法的6種實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05IDEA新建javaWeb以及Servlet簡(jiǎn)單實(shí)現(xiàn)小結(jié)
這篇文章主要介紹了IDEA新建javaWeb以及Servlet簡(jiǎn)單實(shí)現(xiàn)小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11Java啟動(dòng)Tomcat的實(shí)現(xiàn)步驟
本文主要介紹了Java啟動(dòng)Tomcat的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05