天天看点

SpringBoot配置Freemaker视图解析界面SpringBoot配置Freemaker视图解析界面至此SpringBoot集成Freemaker就完成了SpringBoot--------->WebMvcConfigurerAdapter详解至此有关于SpringBoot Freemaker的集成告一段落有需要的同学可以通过下边的实例参考理解

SpringBoot配置Freemaker视图解析界面

上一篇文章中我们简单搭建了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/");
    }

}
           

至此SpringBoot集成Freemaker就完成了

接下来简单说一下MvcConfig.java文件的配置

SpringBoot--------->WebMvcConfigurerAdapter详解

1.WebMvcConfigurerAdapter是什么

Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制

2.WebMvcConfigurerAdapter常用的方法

/**
 * 解决跨域问题
 */
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来处理静态资源

3. 常用的方法详解

3.1 addInterceptors:拦截器

  • addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
  • addPathPatterns:用于设置拦截器的过滤路径规则
  • excludePathPatterns:用于设置不需要拦截的过滤规则
public void addInterceptors(InterceptorRegistry registry) {
    super.addInterceptors(registry);
    registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
}
           

3.2 addCorsMappings:跨域

public void addCorsMappings(CorsRegistry registry) {
    super.addCorsMappings(registry);
    registry.addMapping("/cors/**")
            .allowedHeaders("*")
            .allowedMethods("POST","GET")
            .allowedOrigins("*");
}
           

3.3 addViewControllers:跳转指定页面

public void addViewControllers(ViewControllerRegistry registry) {
     super.addViewControllers(registry);
     registry.addViewController("/").setViewName("/index");
     //实现一个请求到视图的映射,而无需书写controller
     registry.addViewController("/login").setViewName("forward:/index.html");  
}
           

3.4 resourceViewResolver:视图解析器

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
           

二选其一即可

3.5 configureMessageConverters:信息转换器

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配置理解

3.6 addResourceHandlers:静态资源

public void addResourceHandlers(ResourceHandlerRegistry registry) {
 	//处理静态资源的,例如:图片,js,css等
     registry.addResourceHandler("/resource/**").addResourceLocations("/WEB-INF/static/");
 }
           

4.WebMvcConfigurerAdapter使用方式

其实现形式主要有两种,下边依次为大家介绍

4.1 过时方式:继承WebMvcConfigurerAdapter

Spring5.0以后会去掉WebMvcConfigurerAdapter方法

WebMvcConfigurerAdapter是实现的WebMvcConfigurer接口,所以可以直接实现接口WebMvcConfigurerAdapter

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
}
           

不提倡使用

4.2 现用方式

4.2.1 直接实现WebMvcConfigurer接口

@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
	
}
           

4.2.2 继承WebMvcConfigurationSupport

@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {
	
}
           

推荐方式

至此有关于SpringBoot Freemaker的集成告一段落

有需要的同学可以通过下边的实例参考理解

链接:=================SpringBoot集成Freemaker==========

继续阅读