Spring boot 下Freemarker的注解方式配置

本文介绍如何通过注解配置Freemarker,并列举了所需的Maven依赖。文章详细展示了FreemarkerConfig类的实现,包括设置日期格式、自定义标签注入及视图解析器配置。
<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>
@Configuration
public 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一大堆配置。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值