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.但是我们常常会需要 ...
随机推荐
- Python_列表操作1
1.列表相关操作:声明,添加,删除,修改,获取len colors=['红','橙','黄','绿'] #声明一个列表 def colors_getall(): #获取列表中所有元素 return c ...
- ISCC之Re2
硬核rust逆向 首先去学了一天rust...我TMD IDA打开,跟踪主函数 看一下伪代码,发现有一串密文 跟进去发现一串数据,猜测有可能是flag的加密数据,于是回头去分析算法 发现一个关键点 i ...
- Bias vs. Variance(1)--diagnosing bias vs. variance
我们的函数是有high bias problem(underfitting problem)还是 high variance problem(overfitting problem),区分它们很得要, ...
- HDFS中的数据块(Block)
我们在分布式存储原理总结中了解了分布式存储的三大特点: 数据分块,分布式的存储在多台机器上 数据块冗余存储在多台机器以提高数据块的高可用性 遵从主/从(master/slave)结构的分布式存储集群 ...
- 快速开平方取倒数的算法--嵌入式ARM转载
#include<stdio.h> #include<string.h> #include <stdlib.h> /* atof */ /* 计算=1/sqrt(n ...
- 接口中的JSON报文
原始报文: 请求报文: [ { "appNo" : "", "applicantCredentialCode" : "" ...
- python logging 重定向print(标准输入输出)
重定向print输出到Mongo celery 本身用到logging.info 会输出 是celery的问题,还是logging初始化的时候就会有输出? 好像是celery 配合logging的问题 ...
- OAuth 2.0攻击
参考文章:https://www.yuque.com/pmiaowu/web_security_1/oauth 作者:PHPoop 关于OAuth2.0协议的授权流程可以参考下面的流程图: 1.Cli ...
- javaweb学习笔记(三)
一.javaweb高级(Filter和Listener)的简单介绍 1.过滤器Filter (https://www.cnblogs.com/vanl/p/5742501.html) ①定义 Filt ...
- C语言定义结构体指针数组并初始化;里面全是结构体的地址
#include <stdio.h> #include <string.h> struct tells;//声明结构体 struct info { char *infos; } ...