springboot+freemarker 和静态资源路径

依赖

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

application.properties

########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=false
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates/
spring.mvc.static-path-pattern=classpath:/static/**
#comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

在resources下创建static,在这里放静态文件
springboot+freemarker 和静态资源路径_第1张图片
在resources下创建templates,在这里放页面
springboot+freemarker 和静态资源路径_第2张图片
因为有的配置完之后会读取不到静态文件,所以就有了下面

//自定义的静态资源拦截器
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 {
 

    }

}
@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/");

	    }

}

测试类

@Controller
public class testFreemarkerController {
		

	    @RequestMapping("/sysUser")
	    public String user(Model m){
	        
	        return "WeiXin/Sweep-status-1";
	    }
}

页面就不放了,随便写一个就可以,不过下面是根据我的配置静态文件的路径图
在这里插入图片描述

http://localhost:8080/sysUser

springboot+freemarker 和静态资源路径_第3张图片

你可能感兴趣的:(笔记)