今天被问到重定向的问题,后续又引起了静态资源路径配置的问题,在这里做一个总结,当然,顺便添加默认访问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在这里配置静态资源路径,前面说了这里的配置是覆盖默认配置,所以需要将默认的也加上,否则staticpublic等这些路径将不能被当作静态资源路径

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访问静态资源与重定向的更多相关文章

  1. Spring Boot 访问静态资源

    方法1一: 在resources目录下建立static的目录,将静态资源放到此处,可以直接访问 访问:127.0.0.1:9010/img/123.png

  2. spring boot 访问静态资源的步骤

    1)resources/static下放置html文件.(或者static下新建目录 http://hcss:8090/imgs/单例.png) 2)webapp下放置html文件. 如果两个路径下有 ...

  3. Spring boot 默认静态资源路径与手动配置访问路径的方法

    这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下   在application.propertis中配置 ##端口号 ...

  4. spring boot 开静态资源访问,配置视图解析器

    配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...

  5. Spring Boot中静态资源(JS, 图片)等应该放在什么位置

    Spring Boot的静态资源,比如图片应该放在什么位置呢, 如果你放在传统WEB共的类似地方, 比如webapp或者WEB-INF下,你会得到一张示意文件未找到的破碎图片.那应该放哪里呢? 百度一 ...

  6. Spring Boot 的静态资源处理

    做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如何向前端返回静态资源呢?以前做过web开发的同学应该知道,我们以前创建的web工程下面会有一个webapp的目录,我们只要 ...

  7. 不常见偏门的Bug,Spring Boot IDEA 静态资源 图片访问404,初学者之殇

    用过Idea朋友都知道,它有一个非常让人喜欢的功能就是:打算在某个a目录下创建一个hello.class文件,那么你仅需要右键点击New-Java Class- 然后输入名字:a.hello 即可. ...

  8. spring boot 使用静态资源

    ./ 当前目录../ 父级目录/ 根目录 spring boot 打包时: The default includes are as follows: 默认包括的文件 public/**, resour ...

  9. Spring MVC访问静态资源

    http://www.cnblogs.com/yank/p/4477204.html SpringMVC访问静态资源 在SpringMVC中常用的就是Controller与View.但是我们常常会需要 ...

随机推荐

  1. mysqldump 备份与恢复数据库

    备份数据库 mysqldump -u root -plvtao test > /home/bak.sql 数据库还原,常用source 命令登陆 mysql -u root -p mysql&g ...

  2. kubernetes之pod健康检查

    目录 kubernetes之pod健康检查 1.概述和分类 2.LivenessProbe探针(存活性探测) 3.ReadinessProbe探针(就绪型探测) 4.探针的实现方式 4.1.ExecA ...

  3. mysql主从-ms

    一.环境准备 1.准备两台安装有mysql的linux服务器 2.安装的mysql版本最好相同 3.配置两台服务器的主机名和IP地址,主机名:master和slave,IP地址:192.168.0.2 ...

  4. mysqlslap压测

    mysqlslap 是MySQL自带的压测工具: -P --create-schema=test -S /tmp/mysql_sandbox18601.sock --number-of-queries ...

  5. 铁力项目mysql异常处理过程记录

    地区:铁力 故障:2019-06-26 10:19:34 139921514837760 [ERROR] mysqld: Error writing file 'mysql-bin' (errno: ...

  6. javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/pay/index] again. Check your ViewResolver setup!

    2019-08-08 17:12:03.544 ERROR 13748 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Se ...

  7. 07 c++中的内联函数inline

    文章链接: 问题描述:类中成员函数缺省默认是内联的,如果在类定义时就在类内给出函数定义,那当然最好.如果在类中未给出成员函数定义,而又想内联该函数的话,那在类外要加上 inline,否则就认为不是内联 ...

  8. 1-html基本结构与编写规范

    <!DOCTYPE html> <html> <!-- #前端开发系统化学习教程, #包括html.css.pc端及移动端布局技巧.javascript. #jquery ...

  9. (尚011)Vue事件处理

    test011.html <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...

  10. 【转】SignalR与ActiveMQ结合构建实时通信

    一.概述 本教程主要阐释了如何利用SignalR与消息队列的结合,实现不同客户端的交互 SignalR如何和消息队列交互(暂使用ActiveMQ消息队列) SignalR寄宿在web中和其他Signa ...