Springboot中静态文件的引入方式有哪些

发布时间:2022-03-18 13:36:01 作者:小新
来源:亿速云 阅读:156

这篇文章给大家分享的是有关Springboot中静态文件的引入方式有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

thymeleaf 模式

依赖中引入

<!-- 渲染静态页面 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

可选配置

如果你有

WebMvcConfigurationSupport 的一些类引用. 你需要放行他们

Springboot中静态文件的引入方式有哪些

如果你引用了 springSecurity

你也需要放行他们

Springboot中静态文件的引入方式有哪些

Springboot中静态文件的引入方式有哪些

thymeleaf 需要通过controller层转向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进入controller控制器");
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        return mav;
    }
    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public ModelAndView home() {
        System.out.println("/home进入controller控制器");
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        return mav;
    }
    @RequestMapping(value = "/error", method = RequestMethod.GET)
    public ModelAndView error() {
        System.out.println("/error进入controller控制器");
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        return mav;
    }
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login() {
        System.out.println("/login进入controller控制器");
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        return mav;
    }
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView indexs() {
        System.out.println("/ 进入controller控制器");
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        return mav;
    }
    @RequestMapping(value = "/404", method = RequestMethod.GET)
    public ModelAndView error404() {
        System.out.println("/404 进入controller控制器");
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        return mav;
    }
}

yml 做如下配置

Springboot中静态文件的引入方式有哪些

构架这样构架

Springboot中静态文件的引入方式有哪些

Springboot中静态文件的引入方式有哪些

非thymeleaf 模式

首先去掉依赖

Springboot中静态文件的引入方式有哪些

删除controller的指向view层

如果你想带控制器也是可以的   (带的话 指向index. 不带的话 默认指向index .可以理解成一个绝对路径,一个相对路径)

Springboot中静态文件的引入方式有哪些

yml文件中这样配置 

是为了能够直接访问 根目录下的text文件 

Springboot中静态文件的引入方式有哪些

构架如下

Springboot中静态文件的引入方式有哪些

Springboot中静态文件的引入方式有哪些

完成.

Springboot中静态文件的引入方式有哪些

感谢各位的阅读!关于“Springboot中静态文件的引入方式有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. 引入css的方式有几种
  2. 引入JavaScript的方式有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:JavaScript正则表达式中g标志实例分析

下一篇:springboot访问静态资源会遇到什么问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》