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==========

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序小达人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值