今天被问到重定向的问题,后续又引起了静态资源路径配置的问题,在这里做一个总结,当然,顺便添加默认访问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. 宁波市第二届CTF部分WP之msc1,msc2

    msc1签到 这题没啥好说的,修改一下图片宽高,flag到手 msc2 一开始用十六进制编辑器打开,分析文件,暂时无果,卡了一小时(线下没网) 后面,看着这部分文件头眼熟,猜测是GIF头, 于是,在硬 ...

  2. 2013.6.28 - KDD最后一天

    今天收到中秋的邮件.KDD结果出来了,Zhongqiu Wang & Jingwen Huang 15th/561.  

  3. typescript 模块

    模块:模块可以帮助开发者将代码分割为重用的单元.开发者可以自己决定将模块中的哪些资源(类,方法,变量)暴露出去供外部使用,哪些资源只在模块内使用 在ts里面,一个文件就是一个模块,并没有什么特殊的标识 ...

  4. java 实现 单链表

    class Node{ public int val; public Node next; public Node(int val){ this.val=val; } } class LinkList ...

  5. Oracle 中 CONTAINS 函数的用法

    Oracle 中 CONTAINS 函数的用法 1. 查询住址在北京的学生 SELECT student_id,student_name FROM students WHERE CONTAINS( a ...

  6. Python高级编程和异步IO并发编程(笔记)

    一.魔法函数 # 例子 class Company(object): def __init__(self, employee_list): self.employee = employee_list ...

  7. Python文件的读写操作

    Python文件的使用 要点:Python能够以文本和二进制两种形式处理文件. 1.文件的打开模式,如表1:  注意:使用open()函数打开文件,文件使用结束后耀使用close()方法关闭,释放文件 ...

  8. wait,waitpid学习测试

    用man wait学习wait waitpid的使用 wait()函数功能:wait()函数使父进程暂停执行,直到它的一个子进程结束为止,该函数的返回值是终止运行的子进程的PID. 参数status所 ...

  9. mybatis oracle两种方式批量插入数据

    mybatis oracle两种方式批量插入数据 注意insert,一定要添加: useGeneratedKeys="false" ,否者会报错. <insert id=&q ...

  10. GreenPlum failover,primary和mirror切换实验 -- 重要

    GP failover,primary和mirror切换实验 http://blog.sina.com.cn/s/blog_9869114e0101k1nc.html 一.恢复失败的segment出现 ...