springboot 静态样式丢失&css、js访问不到&springboot2.0版本

问题是这样的,我打算写一个单体架构的应用,用到的是springboot+freemarker+uikit(css样式框架)

大体的代码如下图所示(springboot2.0版本)

项目代码位置:码云项目代码demo

文件目录大概框架

springboot 静态样式丢失&css、js访问不到&springboot2.0版本_第1张图片

pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.1.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-freemarker
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


yml配置文件

spring:
  http:
    encoding:
      force: true
      charset: UTF-8
  mvc:
    static-path-pattern: classpath:/static/**
  freemarker:
    allow-request-override: false
    cache: false
    check-template-location: true
    charset: UTF-8
    content-type: text/html; charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .ftl
    template-loader-path: classpath:/templates

TestController-Controller层的编写

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TestController {

    @RequestMapping("/")
    public String test() {
        return "test";
    }

}

.ftl文件编写




    
    
    
    
    
    
    
    
    
    
    
    


计算构件入参列表

当我这么写完代码访问的时候,,http://localhost:8080/    

会得到如下图所示,会发现所有的样式丢失了,这样就有问题了啊,

springboot 静态样式丢失&css、js访问不到&springboot2.0版本_第2张图片

经过查看network发现,所有访问静态文件的路径都失败了,这个就是springboot2.0拦截了static目录下面所有的静态文件

springboot 静态样式丢失&css、js访问不到&springboot2.0版本_第3张图片

 

解决办法:

编写两个class,一个 拦截器

package com.example.demo;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// 自定义的静态资源拦截器
public class ResourceInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
                           ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                Object o, Exception e) throws Exception {

    }
}
package com.example.demo;


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class ResourceConfig implements WebMvcConfigurer {


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ResourceInterceptor()).excludePathPatterns("/static/**");
    }

    @Override
    //需要告知系统,这是要被当成静态文件的!
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 设置文件上传的文件不拦截
//        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ TaleUtils.getUplodFilePath()+"upload/");
        //第一个方法设置访问路径前缀,第二个方法设置资源路径
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

}

这样的话重启项目就会发现不拦截静态文件了

springboot 静态样式丢失&css、js访问不到&springboot2.0版本_第4张图片

 

样式全部加载成功

你可能感兴趣的:(java)