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