Springboot使⽤freemarkerftl引⼊静态资源
我们知道在传统的Spring MVC项⽬中需要配置静态资源⽂件夹才能访问到静态⽂件,boot中同样如此,不过boot追求的少的配置,讲究开箱即⽤,所以boot给我们默认了⼏个静态⽂件路径
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
优先级:META-INF/resources > resources > static > public
⾸先来⽰范⼀下默认的静态资源⽂件夹,这⾥就不做优先级展⽰了,有兴趣的可以⾃⼰建⽴⼏个⽂件夹,然后放不同图⽚且同名的⽂件试⼀下,这⾥就⼀个static⽂件夹
image.png
做⼀个简单的freemarker模板,然后⼀个简单的控制器跳转,特别说明⼀点,做WEB项⽬的时候如果静态资源放在⾃⼰的⼯程静态⽬录下,⽽没有⽤⽂件服务器之类的东西,⼀般不会直接写相对路径,什么../xxx.jpg这种,喜欢标记项⽬路径,在freemarker中也有很多⽅式直接获取到路径,这⾥暂时两种,⼀种是⽤spring.ftl,⼀种是在application.properties配置⽂件中配置,这⾥先展⽰下效果,再说下⽤法。
package com.ller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.wzh.demo.domain.UserBean;spring mvc和boot区别
import com.wzh.demo.service.IUserService;
@Controller
@RequestMapping("/user")
public class userTestController {
@RequestMapping("/img.do")
public String showImg(Model model)
{
model.addAttribute("imgName","test.png");
return "/test/img";
}
}
<#import "spring.ftl" as spring />
<!DOCTYPE html>
<html>
<head>
<title>MyHtml.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<img src="<@spring.url'/imge/${imgName}'/>" width="300px"/>
<img src="${tPath }/imge/${imgName}" width="300px"/>
</body>
</html>
展⽰效果
image.png
在img标签的src 中⽤了两种路径的标记,⼀种是@spring.url,⼀种是tPath。
@spring.url是基于spring-webmvc-4.3.6.RELEASE.jar中的spring.ftl⽂件实现的,其中调⽤rul宏的前缀可以⾃⼰定义,根据你import as 的别名
<#import "spring.ftl" as spring />
我们点开spring.ftl,可以到定义url的⽅法,可以看出就是通过操作上下⽂获得的路径
<#--
* url
*
* Takes a relative URL and makes it absolute from the server root by
* adding the context root for the web application.
-->
<#macro url ><#if extra?? && extra?size!=0>${ContextUrl(relativeUrl,extra)}<#else>${springMacroReque
stCont <#--
image.png
第⼆种是配置⽂件中配置,本质也是通过上下⽂
quest-context-attribute=request
同样的,在开发中因为这样那样的原因,我们需要⾃定义静态资源⽂件夹,下⾯就演⽰⼀下如何⾃定义静态资源⽂件夹,配置的⽅式有两
种,⼀种是在代码中配置,还有⼀种是在application.properties中配置。
为了区别之前的static⽂件夹,新建⼀个webStatic⽂件夹
image.png
这个时候webStatic路径并没有配置为静态资源⽂件夹,因为web的安全机制,我们是⽆法直接通过下⾯这种路径⽅式去访问的,会报404
<img src="${tPath }/pic/${imgName}" width="300px"/>
<img src="${tPath }/webStatic/pic/${imgName}" width="300px"/>
image.png
通过代码配置,在配置注解上这⾥使⽤@Configuration注解,注意不要⽤MVC的@EnableWebMvc, @EnableWebMvc注解会使sping boot默认关于webmvc的配置都会失效
package fig;
import t.annotation.Configuration;
import org.springframework.fig.annotation.ResourceHandlerRegistry;
import org.springframework.fig.annotation.WebMvcConfigurerAdapter;
/**
* Spring MVC 配置类,这⾥只配置静态资源
* 这⾥使⽤的@Configuration注解,注意不要⽤MVC的@EnableWebMvc,
* @EnableWebMvc注解会使sping boot默认关于webmvc的配置都会失效
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
/**
* 重写路径配置⽅法,添加⾃定义的路径
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//将所有/webStatic/** 访问都映射到classpath:/webStatic/ ⽬录下
registry.addResourceHandler("/webStatic/**").addResourceLocations("classpath:/webStatic/");
}
}
测试访问
image.png
测试发现,static⽂件夹下的资源和我们⾃定义的webStatic下的资源都能访问,并不影响Spring Boot的默认映射,可以同时使⽤。不过如果如果我们将/webStatic/**修改为 /**与默认的相同时,则会覆盖系统的配置。
package fig;
import t.annotation.Configuration;
import org.springframework.fig.annotation.ResourceHandlerRegistry;
import org.springframework.fig.annotation.WebMvcConfigurerAdapter;
/**
* Spring MVC 配置类,这⾥只配置静态资源
* 这⾥使⽤的@Configuration注解,注意不要⽤MVC的@EnableWebMvc,
* @EnableWebMvc注解会使sping boot默认关于webmvc的配置都会失效
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
/**
* 重写路径配置⽅法,添加⾃定义的路径
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//将所有/** 访问都映射到classpath:/webStatic/ ⽬录下
registry.addResourceHandler("/**").addResourceLocations("classpath:/webStatic/");
}
}
因为都映射到了webStatic所以不需要⽂件夹前缀了
<img src="<@spring.url'/imge/${imgName}'/>" width="300px"/>
<img src="${tPath }/pic/${imgName}" width="300px"/>
测试访问发现默认配置失效了,所以/**的配置⽅式看实际项⽬情况权衡。