003 spring boot访问静态资源与重定向
今天被问到重定向的问题,后续又引起了静态资源路径配置的问题,在这里做一个总结,当然,顺便添加默认访问index.html。
一:默认访问
1.默认路径
在springboot中静态资源的映射文件是在resources目录下的static文件夹。
2.访问index.html
在项目中,如何访问我们的index.html呢。
首先看程序结构。

然后启动项目。
输入localhost:8090就可以访问了,这个是默认的,不需要具体的写index.html
二:重定向
1.程序
package com.jun; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @SpringBootApplication
@RestController
public class SpringBootApplicationTest {
public static void main(String[] args) {
System.out.print(11);
SpringApplication.run(SpringBootApplicationTest.class,args);
} /**
* 测试重定向,与验证restful
* @param response
* @throws IOException
*/
@GetMapping("/hello")
public void hello(HttpServletResponse response) throws IOException {
response.sendRedirect("http://127.0.0.1:8090/test/tt.html");
}
}
2.知识点
使用response.sendRedirect("绝对路径");
3.注意点
在写绝对路径的时候,可以省略static。
三:资源配置
1.默认资源路径
Spring Boot默认的静态资源文件配置:
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
2.自定义路径的方法
改变springboot项目静态资源文件访问目录的方式有两种:
一种是直接在配置文件中进行设置,
另一种是我们编写一个配置类,继承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter【已经弃用】或者WebMvcConfigurationSupport
并重写addResourceHandlers(ResourceHandlerRegistry registry)方法,其实addResourceHandlers(ResourceHandlerRegistry registry)方法就是个空方法。
3.配置方式
spring.mvc.static-path-pattern=/**表示所有的访问都经过静态资源路径;如果不设置,则不需要在绝对路径中考虑这个配置,如果配置了值,则需要考虑配置的值。在设置之后,在路径中不写配置的值则会报错,找不到路径。
spring.resources.static-locations在这里配置静态资源路径,前面说了这里的配置是覆盖默认配置,所以需要将默认的也加上,否则static、public等这些路径将不能被当作静态资源路径
4.配置方式的实践
classpath下的所有文件都是可以被访问到的。但是这种方式还是不建议的,使用默认的即可,不过实践可以学习一下。
重新设置目录结构:

配置:
server.port=8090
#spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/
在这里,其实可以不设置spring.mvc.static-path-pattern。
程序:
@GetMapping("/hello")
public void hello(HttpServletResponse response) throws IOException {
// response.sendRedirect("http://127.0.0.1:8090/test/tt.html");
response.sendRedirect("http://127.0.0.1:8090/template/yy.html");
}
和:
@GetMapping("/hello")
public void hello(HttpServletResponse response) throws IOException {
// response.sendRedirect("http://127.0.0.1:8090/test/tt.html");
// response.sendRedirect("http://127.0.0.1:8090/template/yy.html");
response.sendRedirect("http://127.0.0.1:8090/static/test/tt.html");
}
5.程序设置
package com.jun.config; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /**
* 这里主要用于设置静态资源的访问路径的测试
*/
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport{
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// 这里之所以多了一"/",是为了解决打war时访问不到问题
registry.addResourceHandler("/**").addResourceLocations("/template","classpath:/template");
}
}
6.访问
上面的程序说明,只能访问template路径下的静态资源。
003 spring boot访问静态资源与重定向的更多相关文章
- Spring Boot 访问静态资源
方法1一: 在resources目录下建立static的目录,将静态资源放到此处,可以直接访问 访问:127.0.0.1:9010/img/123.png
- spring boot 访问静态资源的步骤
1)resources/static下放置html文件.(或者static下新建目录 http://hcss:8090/imgs/单例.png) 2)webapp下放置html文件. 如果两个路径下有 ...
- Spring boot 默认静态资源路径与手动配置访问路径的方法
这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在application.propertis中配置 ##端口号 ...
- spring boot 开静态资源访问,配置视图解析器
配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...
- Spring Boot中静态资源(JS, 图片)等应该放在什么位置
Spring Boot的静态资源,比如图片应该放在什么位置呢, 如果你放在传统WEB共的类似地方, 比如webapp或者WEB-INF下,你会得到一张示意文件未找到的破碎图片.那应该放哪里呢? 百度一 ...
- Spring Boot 的静态资源处理
做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如何向前端返回静态资源呢?以前做过web开发的同学应该知道,我们以前创建的web工程下面会有一个webapp的目录,我们只要 ...
- 不常见偏门的Bug,Spring Boot IDEA 静态资源 图片访问404,初学者之殇
用过Idea朋友都知道,它有一个非常让人喜欢的功能就是:打算在某个a目录下创建一个hello.class文件,那么你仅需要右键点击New-Java Class- 然后输入名字:a.hello 即可. ...
- spring boot 使用静态资源
./ 当前目录../ 父级目录/ 根目录 spring boot 打包时: The default includes are as follows: 默认包括的文件 public/**, resour ...
- Spring MVC访问静态资源
http://www.cnblogs.com/yank/p/4477204.html SpringMVC访问静态资源 在SpringMVC中常用的就是Controller与View.但是我们常常会需要 ...
随机推荐
- pandas速查手册(中文版)
本文翻译自文章:Pandas Cheat Sheet - Python for Data Science 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重要的Python包.它 ...
- elementUI 控制 DatePicker组件 不能选择 设定的日期
<el-date-picker v-model="date" type="daterange" range-separator="至" ...
- npm run build 时的 warning
entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit ...
- myeclipse常用快捷键和小技巧
常用快捷键: Ctrl + Shift + R 在整个项目中查找文件 Ctrl + H 查找文件,可以限定文件中包含的内容 Ctrl + Shift + G 查找一个方法在哪里被调用 Ctrl + O ...
- HDFS集群中DataNode的上线与下线
在HDFS集群的运维过程中,肯定会遇到DataNode的新增和删除,即上线与下线.这篇文章就详细讲解下DataNode的上线和下线的过程. 背景 在我们的微职位视频课程中,我们已经安装了3个节点的HD ...
- Mybatis 不支持通配符扫包起别名问题
typeAliasesPackage 默认只能扫描某一个路径下,或以逗号等分割的 几个路径下的内容,不支持通配符和正则,采用重写的方式解决 package com.xxxx.xxx.util.comm ...
- Cogs 1708. 斐波那契平方和(矩阵乘法)
斐波那契平方和 ★★☆ 输入文件:fibsqr.in 输出文件:fibsqr.out 简单对比 时间限制:0.5 s 内存限制:128 MB [题目描述] ,对 1000000007 取模.F0=0, ...
- 10分钟用Python爬取最近很火的复联4影评
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! <复仇者联盟4:终局之战>已经上映快三个星期了,全球票房破24亿美元,国内票房破40亿人民币. 虽然现在热度逐渐下降,但是我们还 ...
- AtCoder Grand Contest 021题解
传送门 \(A\) 咕咕 ll n,res;bool fl; int main(){ scanf("%lld",&n),fl=1; while(n>9)res+=9, ...
- 记一次清理缓存的小事情(chrome) chrome下清理缓存不生效的问题
记一次清理缓存的小事情(chrome) chrome下清理缓存不生效的问题 前端开发中会经常涉及清理缓存的事情. 在一次开发后, 需要清理缓存,一个哥们怎么清理都不生效, 于是向我求救. 在我看了下后 ...