软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot如何访问html和js等静态资源配置

SpringBoot如何访问html和js等静态资源配置

作者:祗是辉哥哥  发布时间:2023-08-17 08:07:43 

标签:SpringBoot,html,js,静态资源,配置

SpringBoot访问html和js等静态资源配置

把静态资源放到resources/static下,这是springboot静态资源默认访问路径。

在浏览器直接ip:端口/静态资源 就可以了 

下面的废话是好久之前写的,不用看了。。。

SpringBoo推荐使用thymeleaf模板作用前端页面展示,整体结构如下所示:

SpringBoot如何访问html和js等静态资源配置

这里我并没有引入thymeleaf模板,所以页面放在了pages目录下。

application.yml配置如下:

server:
 port: 9000
 context-path: /sso
 address: sso.server

spring:
mvc:
 static-path-pattern: /**
   # 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

我设置了context-path,所以访问的路径前缀都要有/sso。

1:访问html

SpringBoot如何访问html和js等静态资源配置

在controller访问html:

@RequestMapping("/user/login")
   public String login(){
       return "/pages/login.html";
}

注解必须为@controller 

2:访问js

SpringBoot如何访问html和js等静态资源配置

在页面引入js文件:

<script src="/sso/js/jquery-3.3.1.js"></script>

访问该页面显示js文件加载完成。

SpringBoot如何访问html和js等静态资源配置

SpringBoot访问不到js,css等静态资源问题

在网上找了一大部分回答都没解决问题,所以记录一下;

今天碰到这个问题,自己的thymeleaf导入文件的格式也没啥问题,最后点击maven中clean重启下就可以了

来源:https://blog.csdn.net/weixin_42456466/article/details/80688681

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com