溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Springboot中靜態(tài)文件的引入方式有哪些

發(fā)布時(shí)間:2022-03-18 13:36:01 來(lái)源:億速云 閱讀:169 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Springboot中靜態(tài)文件的引入方式有哪些的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來(lái)看看吧。

thymeleaf 模式

依賴中引入

<!-- 渲染靜態(tài)頁(yè)面 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

可選配置

如果你有

WebMvcConfigurationSupport 的一些類引用. 你需要放行他們

Springboot中靜態(tài)文件的引入方式有哪些

如果你引用了 springSecurity

你也需要放行他們

Springboot中靜態(tài)文件的引入方式有哪些

Springboot中靜態(tài)文件的引入方式有哪些

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 做如下配置

Springboot中靜態(tài)文件的引入方式有哪些

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

Springboot中靜態(tài)文件的引入方式有哪些

Springboot中靜態(tài)文件的引入方式有哪些

非thymeleaf 模式

首先去掉依賴

Springboot中靜態(tài)文件的引入方式有哪些

刪除controller的指向view層

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

Springboot中靜態(tài)文件的引入方式有哪些

yml文件中這樣配置 

是為了能夠直接訪問 根目錄下的text文件 

Springboot中靜態(tài)文件的引入方式有哪些

構(gòu)架如下

Springboot中靜態(tài)文件的引入方式有哪些

Springboot中靜態(tài)文件的引入方式有哪些

完成.

Springboot中靜態(tài)文件的引入方式有哪些

感謝各位的閱讀!關(guān)于“Springboot中靜態(tài)文件的引入方式有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI