net.gplatform.sudoor.server.thymeleaf.ThymeleafConfig.java Source code

Java tutorial

Introduction

Here is the source code for net.gplatform.sudoor.server.thymeleaf.ThymeleafConfig.java

Source

package net.gplatform.sudoor.server.thymeleaf;

/*
 * #%L
 * sudoor-server-lib
 * %%
 * Copyright (C) 2013 - 2015 Shark Xu
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.thymeleaf.spring4.resourceresolver.SpringResourceResourceResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

@Configuration
public class ThymeleafConfig implements EnvironmentAware {
    public static final String DEFAULT_PREFIX = "classpath:/templates/";

    public static final String DEFAULT_SUFFIX = ".xml";

    private RelaxedPropertyResolver environment;

    @Autowired
    SpringResourceResourceResolver springResourceResourceResolver;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = new RelaxedPropertyResolver(environment, "spring.thymeleaf.");
    }

    @Bean
    public ITemplateResolver xmlTemplateResolver() {
        TemplateResolver resolver = new TemplateResolver();
        resolver.setResourceResolver(springResourceResourceResolver);
        resolver.setPrefix(this.environment.getProperty("prefix", DEFAULT_PREFIX));
        resolver.setSuffix(DEFAULT_SUFFIX);
        resolver.setTemplateMode(this.environment.getProperty("mode", "xml"));
        resolver.setCharacterEncoding(this.environment.getProperty("encoding", "UTF-8"));
        resolver.setCacheable(this.environment.getProperty("cache", Boolean.class, true));
        return resolver;
    }

}