上一篇文章中我们简单搭建了SpringBoot SSM Demo,接下来为大家简单介绍一下SpeingBoot Freemaker视图集成
链接:SpringBoot SSM框架搭建步骤详解
1.配置pom.xml,添加freemaker的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.配置application.properties文件,添加视图基本配置
#Freemarker
spring.freemarker.enabled=true
spring.freemarker.template-loader-path=classpath:/model/
spring.freemarker.suffix=.html
spring.freemarker.charset=utf-8
3.配置静态资源访问
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
registry.addResourceHandler("/img/**").addResourceLocations("classpath:/img/");
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
}
}
接下来简单说一下MvcConfig.java文件的配置
Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制
/**
* 解决跨域问题
*/
public void addCorsMappings(CorsRegistry registry) ;
/**
* 添加拦截器
*/
void addInterceptors(InterceptorRegistry registry);
/**
* 这里配置视图解析器
*/
void configureViewResolvers(ViewResolverRegistry registry);
/**
* 视图跳转控制器
*/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/**
* 静态资源处理
*/
void addResourceHandlers(ResourceHandlerRegistry registry);
/**
* 默认静态资源处理器
*/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
在上边的配置中,我们就用到了addResourceHandlers来处理静态资源
public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
}
public void addCorsMappings(CorsRegistry registry) {
super.addCorsMappings(registry);
registry.addMapping("/cors/**")
.allowedHeaders("*")
.allowedMethods("POST","GET")
.allowedOrigins("*");
}
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/").setViewName("/index");
//实现一个请求到视图的映射,而无需书写controller
registry.addViewController("/login").setViewName("forward:/index.html");
}
public InternalResourceViewResolver resourceViewResolver()
{
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
//请求视图文件的前缀地址
internalResourceViewResolver.setPrefix("/model/");
//请求视图文件的后缀
internalResourceViewResolver.setSuffix(".html");
return internalResourceViewResolver;
}
此一步配置对应配置文件
spring.freemarker.template-loader-path=classpath:/model/
spring.freemarker.suffix=.html
二选其一即可
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//调用父类的配置
super.configureMessageConverters(converters);
//创建fastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//修改配置返回内容的过滤
fastJsonConfig.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
fastConverter.setFastJsonConfig(fastJsonConfig);
//将fastjson添加到视图消息转换器列表内
converters.add(fastConverter);
}
此一步主要用于ajax json下载,可对比SpringMVC配置理解
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//处理静态资源的,例如:图片,js,css等
registry.addResourceHandler("/resource/**").addResourceLocations("/WEB-INF/static/");
}
其实现形式主要有两种,下边依次为大家介绍
Spring5.0以后会去掉WebMvcConfigurerAdapter方法
WebMvcConfigurerAdapter是实现的WebMvcConfigurer接口,所以可以直接实现接口WebMvcConfigurerAdapter
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
}
不提倡使用
@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
}
@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {
}
推荐方式
链接:=================SpringBoot集成Freemaker==========