Thymeleaf html代码使用nginx与java代码分离

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/helenyqa/article/details/61931111

Thymeleaf html代码使用nginx与java代码分离

官方文档

http://www.thymeleaf.org/apidocs/thymeleaf/3.0.3.RELEASE/

XML

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

方法1、JAVA代码

    @Configuration
    public class ThymeleafConfig{
        @Autowired
        private SpringTemplateEngine springTemplateEngine;

        String resourcesUrl = "http://127.0.0.1";

        public void init(){
        }

        @PostConstruct
        public void templateResolver() {
            UrlTemplateResolver resolver = new UrlTemplateResolver();
            resolver.setTemplateMode("HTML5");
            resolver.setPrefix(resourcesUrl + "/resources/");
            resolver.setSuffix(".html");
            resolver.setCacheable(thymeleafCache);
            resolver.setOrder(1);
            resolver.setCharacterEncoding("UTF-8");
            springTemplateEngine.setTemplateResolver(resolver);
        }

    }

方法2、配置文件

        spring.thymeleaf.cache=false
        spring.thymeleaf.encoding=UTF-8
        spring.thymeleaf.mode=HTML5
        spring.thymeleaf.prefix=http://127.0.0.1/resources/
        spring.thymeleaf.enabled=true

NGINX配置

        server {
                listen       80;
                server_name   www.xxx.com;
                charset utf-8;
                location /resources/ {
                     root D:\workspaces;
                 }
         }

刷新html

    @Autowired
    private SpringTemplateEngine springTemplateEngine;
    @RequestMapping(value = "/thymeleaf/cache/clear", method RequestMethod.GET)
    @ResponseBody
    public String initModifyCell(@RequestParam("name") String tmpName, Model model) throws Exception {
        log.debug("handling clear tmp cache, tmpName={}", tmpName);
        if ("ALL".equals(tmpName)) {
            springTemplateEngine.clearTemplateCache();
        } else {
            springTemplateEngine.clearTemplateCacheFor(tmpName);
        }
        return "Thymeleaf template cache cleared successfully!";
    }

源码

    /*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2016, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package org.thymeleaf.templateresolver;

import java.net.MalformedURLException;
import java.util.Map;
import java.util.regex.Pattern;

import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.cache.ICacheEntryValidity;
import org.thymeleaf.cache.NonCacheableCacheEntryValidity;
import org.thymeleaf.templateresource.ITemplateResource;
import org.thymeleaf.templateresource.UrlTemplateResource;

/**
 * <p>
 *   Implementation of {@link ITemplateResolver} that extends {@link AbstractConfigurableTemplateResolver}
 *   and creates {@link UrlTemplateResource} instances for template resources.
 * </p>
 * <p>
 *   Note a class with this name existed since 1.0, but it was completely rewritten in Thymeleaf 3.0.
 * </p>
 *
 * @author Daniel Fern&aacute;ndez
 *
 * @since 3.0.0
 *
 */
public class UrlTemplateResolver extends AbstractConfigurableTemplateResolver {

    private static final Pattern JSESSIONID_PATTERN = Pattern.compile("(.*?);jsessionid(.*?)");

    public UrlTemplateResolver() {
        super();
    }

    @Override
    protected ITemplateResource computeTemplateResource(
            final IEngineConfiguration configuration, final String ownerTemplate, final String template, final String resourceName, final String characterEncoding, final Map<String, Object> templateResolutionAttributes) {
        try {
            return new UrlTemplateResource(resourceName, characterEncoding);
        } catch (final MalformedURLException ignored) {
            return null;
        }
    }

    @Override
    protected ICacheEntryValidity computeValidity(final IEngineConfiguration configuration, final String ownerTemplate, final String template, final Map<String, Object> templateResolutionAttributes) {
        /*
         * This check is made so that we don't fill the cache with entries for the same
         * template with different jsessionid values.
         */
        if (JSESSIONID_PATTERN.matcher(template.toLowerCase()).matches()) {
            return NonCacheableCacheEntryValidity.INSTANCE;
        }
        return super.computeValidity(configuration, ownerTemplate, template, templateResolutionAttributes);
    }

}
阅读更多
换一批

没有更多推荐了,返回首页