Spring boot 下Freemarker的注解方式配置
来源:互联网 发布:淘宝客手机版 编辑:程序博客网 时间:2024/06/02 01:19
<span style="font-size:18px;"><span style="color:#3333FF;">1.创建FreemarkerConfig使用@Configuration将Freemarker需要的配置注入</span>import javax.annotation.PostConstruct;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;<span style="color:#006600;">//import com.gstar.acp.portal.directive.article.ArticleDirective;//import com.gstar.acp.portal.directive.channel.ChannelListDirective;//import com.gstar.acp.portal.directive.content.ContentListDirective;</span>import freemarker.template.TemplateException;<span style="color:#009900;">/** * @author chenmd * */</span>@Configurationpublic class FreeMarkerConfig { @Autowired protected freemarker.template.Configuration configuration; @Autowired protected org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver resolver; @Autowired protected org.springframework.web.servlet.view.InternalResourceViewResolver springResolver; @PostConstruct public void setSharedVariable(){ configuration.setDateFormat("yyyy/MM/dd"); configuration.setDateTimeFormat("yyyy-MM-dd HH:mm:ss"); <span style="color:#009900;">//下面三句配置的就是我自己的freemarker的自定义标签,在这里把标签注入到共享变量中去就可以在模板中直接调用了 //configuration.setSharedVariable("content_list", new ContentListDirective()); //configuration.setSharedVariable("article_list", new ArticleDirective()); //configuration.setSharedVariable("channel_list", new ChannelListDirective());</span> <span style="color:#009900;">/** * setting配置 */</span> try { configuration.setSetting("template_update_delay", "1"); configuration.setSetting("default_encoding", "UTF-8"); } catch (TemplateException e) { e.printStackTrace(); } <span style="color:#009900;">/** * 配置Spring JSP的视图解析器 */</span> springResolver.setPrefix("/XXX/");<span style="color:#009900;">//解析前缀后XXX路径下的jsp文件</span> springResolver.setSuffix(".jsp"); springResolver.setOrder(1); <span style="color:#33CC00;">/** * 配置Freemarker视图解析器 */</span> resolver.setSuffix(".html"); <span style="color:#009900;">//解析后缀为html的</span> resolver.setCache(false); <span style="color:#009900;">//是否缓存模板</span> resolver.setRequestContextAttribute("request"); <span style="color:#009900;">//为模板调用时,调用request对象的变量名</span> resolver.setOrder(0); } }</span>
(注:以上就是Freemarker用注解的方式替代了以前的使用XML配置文件繁琐的配置方式,至于在pom.xml中引入freemarker的依赖,可以百度搜索 maven spring boot freemarker,添加到自己的pom.xml依赖中去就可以了)
<!----------------------------------------------------2016-7-6 10:29:02------------------------------------------------------------------->
2.下面说描述下pom.xml中需要添加的freemarker的依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><pre name="code" class="html"> </dependencies>
OK,添加完依赖后更新maven project ,freemarker的配置就配置完成了,不用像以前一样配置繁琐的xml一大堆配置。
1 0
- Spring boot 下Freemarker的注解方式配置
- spring boot配置freemarker--autoIncludes
- Spring boot配置使用Freemarker
- Spring boot freemarker模板路径的几种设置方式
- spring boot框架学习学前掌握之重要注解(1)-sprng的java配置方式
- spring boot框架学习学前掌握之重要注解(2)-通过java的配置方式进行配置spring
- Spring Boot配置方式
- Spring boot下配置使用redis--注解形式
- spring boot的quartz的配置方式
- spring boot 注解配置redis
- 注解方式配置spring
- spring boot的Java配置方式
- spring boot 配置freemarker 异常处理
- Spring boot配置freemarker,jsp,velocity模板
- Spring 注解的方式的配置
- Spring Boot 注解启动的两种方式
- Spring Boot 注解启动的两种方式
- spring中aop的注解配置方式
- 深入理解android UI Thread
- artTemplate.js 实例
- 字符截取命令-awk命令
- 现代OpenGL教程 01 - 入门指南
- DB2和 Oracle的并发控制(锁)比较
- Spring boot 下Freemarker的注解方式配置
- Struts2中过滤器和拦截器的区别
- Spring的优点,方便记忆版
- couldn’t be opened because you don’t have permission to view it.
- C++经典书目
- 设计模式——命令模式实现undo、redo
- eclipse设置新建jsp文件默认字符编码
- hdu:1754
- Visual studio2010不显示.svg格式图片