如果 我们的项目是一个jar工程,那么就没有webapp,我们的静态资源该放哪里呢?
应为在很多时候前端是和后端分离的 前端通过Ajax http请求就行了
而我们后端通过 Controller 层 反馈给前端
而大多数的 静态资源都是在后端存储着
在Springboot中有一个叫做ResourceProperties的类,里面就定义了静态资源的默认查找路径:
默认的静态资源路径为:
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
只要静态资源放在这些目录中任何一个,Springboot都会帮我们处理。
我们习惯会把静态资源放在classpath:/static/目录下。
classpath:就是resources
访问的路径 开头必须是绝对路径 /
比如: /css/xx.css 会自动进入 上面那些目录中找
不能是相对路径 比如: css/xxx.css 这样就会出现 404
如果使用 还是访问不到 那么配置过滤器
package com.fastdfs.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//请求 放行 静态资源放行
@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
.allowCredentials(true).maxAge(3600);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**","/templates/**","/public/**")
.addResourceLocations("classpath:/static/","classpath:/public/");
}
}
我们创建static目录进行测试,并且添加一些静态资源:
如果没有那么就多启动几次 或者将 target删除 从新自动生成
然后在网页中测试
http://localhost/timg.jpg
那么我在static中在创建一层img 里面放了一个 timg111.jpg图片
http://localhost/img/timg111.jpg
以此类推
我们以前使用sprongmvc 都是在webapp下 放置前端页面 和静态资源
而在MVC中 视图解析器是: 前缀 /WEB-INF/jsp/ 后缀 .jsp
如果我想在springboot中跟SpringMVC中一样的效果 那么按照下面的配置 就行
<!--Sprngboot 启动器 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- servlet 依赖包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<!-- JSTL (JSP standard Tag Library) JSP 标准标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Tomcat的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<!-- jsp页面的支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
</dependencies>
然后在application.yml配置文件里
spring:
mvc:
view:
prefix: /WEB-INF/jsp/ #默认前缀目录
suffix: .jsp #文件默认后缀
这样我们就配置好了在Springboot -> Springmvc 视图解析器了
springboot 访问webapp下的js,css jsp等 这些资源
因为我们创建项目的时候 没有使用模板直接 裸创建了一个Maven项目 所以需要自己创建webapp
(如果你使用 Maven web模板创建的项目 跳过这一步 默认都给你配置好了)
然后配置 (如果你使用 Maven web模板创建的项目 跳过这一步 默认都给你配置好了) 建议自己去看看
如何访问到webapp下的这些css、img、js中的静态资源呢?
pom.xml
在</build> 里添加以下代码
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
这样,springboot才会将对应资源编译到target目录中 但是还不能使用 应为springboot默认就只会找上面介绍的那几个目录下的文件 所以需要我们映射
application.yml配置文件内
static-path-pattern: /// /#放行全部访问路径
到此 基本上 所以静态资源都能访问了
但是面临一个新的问题 就是在 Springmvc
无论是 http://localhost:8080 还是http://localhost:8080/index.jsp 都能进入到index.jsp 页面中 这个怎么实现呢
在SpringBoot 更为强大
添加Maven
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
然后 创建MyMvcConfig 拦截器
package cn.test.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//一系列实现方法
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//如果你的请求是 http://localhost:8080 那么自动跳转到 登录页面
registry.addViewController("/").setViewName("login");
//我们还可以这样 如果你的请求是 http://localhost:8080/index.jsp 那么自动跳转到 登录页面
//registry.addViewController("/index.jsp").setViewName("login");
//等等 更具你自己的需求 一般上面那个就够了 我们会用其他方式进行拦截的 比如 Security 技术..
}
}
点赞 -收藏-关注-便于以后复习和收到最新内容有其他问题在评论区讨论-或者私信我-收到会在第一时间回复如有侵权,请私信联系我感谢,配合,希望我的努力对你有帮助^_^
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_45203607/article/details/120250785
内容来源于网络,如有侵权,请联系作者删除!