SpringBoot中關于static和templates的注意事項以及webjars的配置
1. 默認情況下, 網頁存放于static目錄下, 默認的"/"指向的是~/resouces/static/index.html文
2. 如果引入了thymeleaf, 則默認指向的地址為~/resouces/templates/index.html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
代碼結構:

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時, 需導入類似下面的包
<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. 結果:

總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
相關文章
Spring中Bean創(chuàng)建完后打印語句的兩種方法
這篇文章主要介紹了Spring中Bean創(chuàng)建完后打印語句的兩種方法,一個是實現InitializingBean接口,另一個使用@Bean注解和initMethod屬性,通過代碼示例介紹的非常詳細,感興趣的小伙伴可以參考閱讀2023-07-07
SpringBoot通過整合Dubbo解決@Reference注解問題
這篇文章主要介紹了SpringBoot通過整合Dubbo解決@Reference注解問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
3行代碼快速實現Spring Boot Oauth2服務功能
oauthserver是一個基于Spring Boot Oauth2的完整的獨立的Oauth服務器。僅僅需要創(chuàng)建相關數據表,修改數據庫的連接信息,你就可以得到一個Oauth服務器。這篇文章給大家介紹3行代碼快速實現Spring Boot Oauth2服務功能,需要的朋友參考下吧2018-04-04

