1 package com.yy.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.ComponentScan; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.web.servlet.ViewResolver; 7 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; 8 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 10 import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; 11 import org.springframework.web.servlet.view.freemarker.FreeMarkerView; 12 import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; 13 14 import java.util.Properties; 15 16 /** 17 * 配置上下文加载需要的bean 18 * 包含Web组件的bean,如控制器、视图解析器、处理器映射 19 * User : liuhuangyinying 20 * Date : 2017-03-21 15:48 21 */ 22 @Configuration 23 @EnableWebMvc 24 @ComponentScan("com.yy") // 启动组件扫描 25 public class WebConfig extends WebMvcConfigurerAdapter{ 26 27 /** 28 * 配置静态文件处理 29 */ 30 @Override 31 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 32 configurer.enable("/WEB-INF/static/"); 33 } 34 35 /** 36 * 配置freemarker视图解析器 37 */ 38 @Bean 39 public ViewResolver viewResolver(){ 40 FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver(); 41 viewResolver.setViewClass(FreeMarkerView.class); 42 viewResolver.setCache(false); 43 viewResolver.setPrefix("/WEB-INF/template/"); 44 viewResolver.setSuffix(".ftl"); 45 viewResolver.setContentType("text/html;charset=utf-8"); 46 viewResolver.setExposeRequestAttributes(true); 47 viewResolver.setExposeSessionAttributes(true); 48 viewResolver.setExposeSpringMacroHelpers(true); 49 viewResolver.setRequestContextAttribute("rc"); 50 return viewResolver; 51 } 52 53 @Bean 54 public FreeMarkerConfigurer freeMarkerConfigurer(){ 55 FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); 56 freeMarkerConfigurer.setTemplateLoaderPath("/"); 57 freeMarkerConfigurer.setDefaultEncoding("UTF-8"); 58 Properties properties = new Properties(); 59 properties.put("template_update_delay",10); 60 properties.put("defaultEncoding","UTF-8"); 61 properties.put("url_escaping_charset","UTF-8"); 62 properties.put("locale","zh_CN"); 63 properties.put("boolean_format","true,false"); 64 properties.put("time_format","HH:mm:ss"); 65 properties.put("datetime_format","yyyy-MM-dd HH:mm:ss"); 66 properties.put("date_format","yyyy-MM-dd"); 67 properties.put("number_format","#,###.##"); 68 properties.put("whitespace_stripping","true"); 69 properties.put("classic_compatible","true"); 70 freeMarkerConfigurer.setFreemarkerSettings(properties); 71 return freeMarkerConfigurer; 72 } 73 }
配置后启动项目,项目居然报错
居然是空指针错误,那么就只能设置断点调试,看看是否缺少配置什么属性。
跟踪到这一步的时候发现,这里抛出的异常,那么继续往下调试:
进入props.setProperty方法,会发现进入到java.util.Properties类报错:
什么鬼,只要不是String类型就设置为null,也就是说不支持int和boolean类型。
也就是说这里的value都必须加上双引号。不是经典的java包就是百分百可以信任的,遇到问题仍然要看源码才可以解决问题。
错误提示特别是空指针不一定是根源,断点真是个好东西。Love Love Love