1、如何设置web静态页面的路径addResourceHandlers

springboot默认的静态页面是resources/static文件夹

如果集成WebMvcConfigurationSupport这个方法,就必须自己设定静态页面路径。否则无法访问静态页面路径。
如果想改其他的路径才会用到如下的方法

  1. @Override
  2. protected void addResourceHandlers(ResourceHandlerRegistry registry) {
  3. log.info("开始进行静态资源映射");
  4. registry.addResourceHandler("/**")
  5. .addResourceLocations("classpath:/static/");
  6. }

classpath变量是什么意思

参考:https://segmentfault.com/a/1190000015802324
java和和resources在编译后会编程classes文件夹下的com和static对应的文件,所以classpath可以理解为java或resources文件夹
image.png

2、设置默认的访问主页index.html

参考:Spring Boot 默认跳转到index.html的二种方式
springboot 2.3.2如何从后台以请求至vue前端页面
HttpServletResponse.sendRedirect这个方法,实现跳转页面

  1. package com.tj.demo.system.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. @Controller
  8. @RequestMapping("/")
  9. public class IndexController {
  10. /**
  11. * Get访问根路径是跳转到index.html页面
  12. *
  13. * @param response
  14. * @throws IOException
  15. */
  16. @GetMapping
  17. public void toIndex(HttpServletResponse response) throws IOException {
  18. response.sendRedirect("/index.html");
  19. }
  20. }

附录:完整配置文件WebMvcConfig

  1. package com.tj.demo.config;
  2. import com.tj.demo.utils.JacksonObjectMapper;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.http.converter.HttpMessageConverter;
  6. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  7. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  8. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  9. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
  10. import java.util.List;
  11. @Slf4j
  12. @Configuration
  13. public class WebMvcConfig extends WebMvcConfigurationSupport {
  14. /**
  15. * springboot默认的静态页面是static,
  16. * 但是如果集成WebMvcConfigurationSupport后,就必须指定设置web静态资源映射
  17. * 否则无法访问html的页面
  18. * "/**"表示根路径,访问路径例如:http://localhost:8084/index.html
  19. * 如果设置"/web/**",那么访问连接就要加上/web,例如:http://localhost:8084/web/index.html
  20. *
  21. * @param registry
  22. */
  23. @Override
  24. protected void addResourceHandlers(ResourceHandlerRegistry registry) {
  25. log.info("开始进行静态资源映射");
  26. registry.addResourceHandler("/**")
  27. .addResourceLocations("classpath:/static/");
  28. // registry.addResourceHandler("/templates/**")
  29. // .addResourceLocations("classpath:/template/");
  30. }
  31. /**
  32. * 扩展MVC框架的消息转换器
  33. *
  34. * @param converters
  35. */
  36. @Override
  37. protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  38. //创建消息转换器对象
  39. MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
  40. //设置对象转换器,底层使用Jackson将Java对象转为json
  41. messageConverter.setObjectMapper(new JacksonObjectMapper());
  42. //将上面的消息转换对象追加到mvc框架的转换器集合中
  43. converters.add(0, messageConverter);
  44. }
  45. }