springboot自定义freemarker的FreeMarkerViewResolver
解决方法:
1、在sringboot的启动类Application中添加以下bean
@Bean
public CommandLineRunner customFreemarker(final FreeMarkerViewResolver resolver) {
return new CommandLineRunner() {
public void run(String... strings) throws Exception {
resolver.setViewClass(MyFreemarkerView.class);
//可以在这里写自定义页面配置
}
};
}
2、MyFreemarkerView类
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
public class MyFreemarkerView extends FreeMarkerView {
public MyFreemarkerView() {
}
protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws
Exception {
model.put("yml", "全局变量,可以在ftl中使用");
super.exposeHelpers(model, request);
}
}
本文链接:http://www.yayihouse.com/yayishuwu/chapter/1674