Springboot中靜態(tài)文件的兩種引入方式總結(jié)
thymeleaf 模式
依賴中引入
<!-- 渲染靜態(tài)頁面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
可選配置
如果你有
WebMvcConfigurationSupport 的一些類引用. 你需要放行他們

如果你引用了 springSecurity
你也需要放行他們


thymeleaf 需要通過controller層轉(zhuǎn)向view 層
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
/**
* @ClassName:
* @Descripton:
* @Author: sansy
* @Date: 2019/5/16 10:12
* @Version: 2.0
*/
@RestController
public class IndexController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView index() {
System.out.println("/index進(jìn)入controller控制器");
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
@RequestMapping(value = "/home", method = RequestMethod.GET)
public ModelAndView home() {
System.out.println("/home進(jìn)入controller控制器");
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
@RequestMapping(value = "/error", method = RequestMethod.GET)
public ModelAndView error() {
System.out.println("/error進(jìn)入controller控制器");
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login() {
System.out.println("/login進(jìn)入controller控制器");
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView indexs() {
System.out.println("/ 進(jìn)入controller控制器");
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
@RequestMapping(value = "/404", method = RequestMethod.GET)
public ModelAndView error404() {
System.out.println("/404 進(jìn)入controller控制器");
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
}
yml 做如下配置

構(gòu)架這樣構(gòu)架


非thymeleaf 模式
首先去掉依賴

刪除controller的指向view層
如果你想帶控制器也是可以的 (帶的話 指向index. 不帶的話 默認(rèn)指向index .可以理解成一個(gè)絕對路徑,一個(gè)相對路徑)

yml文件中這樣配置
是為了能夠直接訪問 根目錄下的text文件

構(gòu)架如下


完成.

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis plus表的創(chuàng)建時(shí)間和修改時(shí)間的操作方法
這篇文章主要介紹了mybatis plus表的創(chuàng)建時(shí)間和修改時(shí)間的實(shí)現(xiàn)方法,本文給大家分享兩種方法,每種方法通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
SpringCloud Gateway自動(dòng)裝配實(shí)現(xiàn)流程詳解
Spring Cloud Gateway旨在為微服務(wù)架構(gòu)提供一種簡單有效的、統(tǒng)一的 API 路由管理方式。Spring Cloud Gateway 作為 Spring Cloud 生態(tài)系中的網(wǎng)關(guān),它不僅提供統(tǒng)一的路由方式,并且基于 Filter 鏈的方式提供了網(wǎng)關(guān)基本的功能,例如:安全、監(jiān)控/埋點(diǎn)和限流等2022-10-10

