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

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);
    }

}
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Thymeleaf中设置动态轮播图,您需要使用Thymeleaf与Spring MVC集成来获取动态数据,并将其传递到模板中。然后,您可以使用JavaScript库来创建动态轮播组件。 以下是一个使用Bootstrap和Spring MVC的动态轮播图示例代码: 1. 在Spring MVC控制器中获取动态数据(例如,从数据库中检索图像路径)并将其传递给模板。 ```java @GetMapping("/carousel") public String getCarouselData(Model model) { // 获取图像路径列表,并将其传递到模板 List<String> imagePathList = getImagePathList(); model.addAttribute("imagePathList", imagePathList); return "carousel"; } ``` 2. 在Thymeleaf模板中使用Thymeleaf表达式循环遍历图像路径列表,并将它们传递给JavaScript轮播组件。 ```html <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <li th:each="imagePath, index : ${imagePathList}" data-target="#carouselExampleIndicators" th:data-slide-to="${index}" th:classappend="${index == 0} ? 'active'"> </li> </ol> <div class="carousel-inner"> <div th:each="imagePath, index : ${imagePathList}" class="carousel-item" th:classappend="${index == 0} ? 'active'"> <img th:src="${imagePath}" alt="Slide"> </div> </div> <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> ``` 在上面的代码中,我们使用Thymeleaf表达式`${}`来引用控制器中传递的`imagePathList`属性,并使用Thymeleaf的`th:each`属性来循环遍历图像路径列表。我们还使用了`th:classappend`属性来动态设置`active`类,以便在轮播中仅显示一个图像。 希望这个示例能够帮助您在Thymeleaf中实现动态轮播图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值