SpringBoot中關(guān)于static和templates的注意事項(xiàng)以及webjars的配置
1. 默認(rèn)情況下, 網(wǎng)頁(yè)存放于static目錄下, 默認(rèn)的"/"指向的是~/resouces/static/index.html文
2. 如果引入了thymeleaf, 則默認(rèn)指向的地址為~/resouces/templates/index.html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
代碼結(jié)構(gòu):

3.在引入thymeleaf后, 如果仍需要訪問(wèn)~/static/index.html, 則可以使用重定向
return "redirect:/index.html"
代碼樣例:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class HomeCtrl {
@GetMapping("/")
public String homePage(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
return "/index";
}
@RequestMapping("/static")
public String navigatorToStatic() {
return "redirect:/static.html";
}
<!DOCTYPE html>
<html>
<head>
<script src="webjars/jquery/3.1.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" />
</head>
<body>
<div class="container"><br/>
<div class="alert alert-success">
Hello, <strong>BootStarp & WebJars!</strong>
</div>
</div>
</body>
</html>
4. HTML中引入webjars時(shí), 需導(dǎo)入類似下面的包
<dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.7</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.1.1</version> </dependency>
5. HTML樣例
<!DOCTYPE html>
<html>
<head>
<script src="webjars/jquery/3.1.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" />
</head>
<body>
<div class="container"><br/>
<div class="alert alert-success">
Hello, <strong>BootStarp & WebJars!</strong>
</div>
</div>
</body>
</html>
6. 結(jié)果:

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Spring中Bean創(chuàng)建完后打印語(yǔ)句的兩種方法
這篇文章主要介紹了Spring中Bean創(chuàng)建完后打印語(yǔ)句的兩種方法,一個(gè)是實(shí)現(xiàn)InitializingBean接口,另一個(gè)使用@Bean注解和initMethod屬性,通過(guò)代碼示例介紹的非常詳細(xì),感興趣的小伙伴可以參考閱讀2023-07-07
SpringBoot通過(guò)整合Dubbo解決@Reference注解問(wèn)題
這篇文章主要介紹了SpringBoot通過(guò)整合Dubbo解決@Reference注解問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
3行代碼快速實(shí)現(xiàn)Spring Boot Oauth2服務(wù)功能
oauthserver是一個(gè)基于Spring Boot Oauth2的完整的獨(dú)立的Oauth服務(wù)器。僅僅需要?jiǎng)?chuàng)建相關(guān)數(shù)據(jù)表,修改數(shù)據(jù)庫(kù)的連接信息,你就可以得到一個(gè)Oauth服務(wù)器。這篇文章給大家介紹3行代碼快速實(shí)現(xiàn)Spring Boot Oauth2服務(wù)功能,需要的朋友參考下吧2018-04-04
Java應(yīng)用注冊(cè)成Windows服務(wù)實(shí)現(xiàn)自啟的教程詳解
這篇文章主要給大家介紹了Java應(yīng)用注冊(cè)成Windows服務(wù)實(shí)現(xiàn)自啟的教程,文中有詳細(xì)的代碼示例和圖文講解供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-02-02
如何基于java隨機(jī)獲取不重復(fù)數(shù)值
這篇文章主要介紹了如何基于java隨機(jī)獲取不重復(fù)數(shù)值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

