最近团队的一个项目在重构,希望引入Thymeleaf减少页面端的代码复杂性。在重构过程中,发现html文件需要保存在多个不同的目录中,但Thymeleaf缺省的实现不支持这种方式。

1        背景

Maven项目,前端使用SpringMVC,没有使用任何模板引擎。所有的页面内容,都是通过静态HTML+AJAX+JSON形式实现。

1.1     项目结构

Html文件,通过mvc:resource 定义路径。

1.1.1            Html保存路径

/hardess_finance/src/main/webapp/WEB-INF/htmls

在该目录放一个Demo文件/demo/hello.html

1.1.2            Spring配置文件

<mvc:resources mapping="/**/**.html" location="/WEB-INF/htmls/"/>

项目启动后,浏览器访问 http://localhost:8080/demo.hello.html,就可以访问到demo文件。

1.2     添加Thymeleaf支持

Spring Boot 项目缺省使用Thymeleaf模板,但普通SpringMVC项目,需要手工添加支持。大致步骤包括:

1.2.1            Pom.xml增加thymeleaf dependency

<dependency>

<groupId>org.thymeleaf</groupId>

<artifactId>thymeleaf</artifactId>

<version>3.0.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.thymeleaf</groupId>

<artifactId>thymeleaf-spring3</artifactId>

<version>3.0.6.RELEASE</version>

</dependency>

1.2.2            修改Spring Config文件

<bean id="templateResolver"

class="org.thymeleaf.spring3.templateresolver.SpringResourceTemplateResolver">

<property name="prefix"><value>/WEB-INF/html/</value></property>

<property name="suffix"><value>.html</value></property>

<property name="templateMode"><value>HTML</value></property>

<property name="characterEncoding"><value>UTF-8</value></property>

<property name="cacheable" value="false"/>

</bean>

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">

<property name="templateResolver" ref="templateResolver" />

</bean>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">

<property name="templateEngine" ref="templateEngine" />

<property name="characterEncoding" value="UTF-8"/>

</bean>

<property name="prefix"><value>/WEB-INF/html/</value></property>这个参数,指定的就是html文件的保存路径。

1.2.3            添加Thymeleaf Controller

在代码中增加一个Controller

@Controller

public class ThymeleafCommonController {

@RequestMapping(value = { "/**/**.html" })

public ModelAndView index(HttpServletRequest request) {

return new ModelAndView();

}

}

至此,项目重新启动后,所有html的访问,都已经通过Thymeleaf引擎,html中能够使用 th:text 等各种thymeleaf语法。

1.3     项目重构希望添加另一个html保存路径

在重构过程中,希望将html文件保存到新的目录 src/main/resources/templates目录,原因有二:

1、              这是Spring Boot项目的缺省模板路径,适应将来可能升级到SpringBoot的需求。

2、              重构后的代码,希望同原目录有所区分,简化开发复杂度。

1.3.1            简单尝试

在 mvc:resources标签中,location可以是用逗号隔开的多个路径,如

<mvc:resources mapping="/scripts/**"

location="/WEB-INF/scripts/, classpath:/static/scripts/"/>

因此,尝试在spring config配置文件中,尝试修改配置

<property name="prefix">

<value>/WEB-INF/html/,classpath:/templates/</value>

</property>

重启后测试,发现项目无法工作,原有的界面都无法加载了。

1.3.2            原因

Debug了一下thymeleaf的相关源码,发现它使用下面的语句生成最终的完整路径名,并没有判断 prefix 是否是逗号分隔的数组。

AbstractConfigurableTemplateResolver.computeResourceName(…)

return prefix + unaliasedName + suffix;

2        Thymeleaf源码解读

解读Thymeleaf的源代码,发现几个相关类

2.1     相关类结构

2.2     final computeTemplateResource()

这个函数会读取配置的prefix,并调用后续方法生成 resource name。注意,这个方式是 final ,无法重载。

@Override

protected final ITemplateResource computeTemplateResource(

final IEngineConfiguration configuration, final String ownerTemplate,

final String template, final Map<String, Object> templateResolutionAttributes) {

final String resourceName =

computeResourceName(configuration, ownerTemplate, template, this.prefix,

this.suffix, this.forceSuffix, this.templateAliases, templateResolutionAttributes);

return computeTemplateResource(configuration, ownerTemplate, template, resourceName,

this.characterEncoding, templateResolutionAttributes);

}

标红部分读取prefix参数值。

2.3     computeResourceName()

实际生成resource name(代码有删减,只保留核心部分)

protected String computeResourceName(

final IEngineConfiguration configuration, final String ownerTemplate, final String template,

final String prefix, final String suffix, final boolean forceSuffix,

final Map<String, String> templateAliases, final Map<String, Object> attributes) {

String unaliasedName = templateAliases.get(template);

if (unaliasedName == null) {

unaliasedName = template;

}

// hasPrefix && shouldApplySuffix

return prefix + unaliasedName + suffix;

}

2.4     computeResolvable()

判断资源文件是否可用。

if (this.resolvablePatternSpec.isEmpty()) {

return true;

}

return this.resolvablePatternSpec.matches(template);

这个代码,实际没有校验html文件是否存在,只要语法不出错即可。当系统定义了多个ITemplateResolver时,引擎回依次调用每个实例的computeResolvable()方法,如果返回null,则依次检查下一个resolver,直到得到一个非空值。

3        解决方案

基于前面的代码分析,要解决我们的需求,首先我们需要解决的是判断资源文件是否真实存在。

3.1     判断文件是否存在

通过Spring项目的ApplicationContext判断文件是否存在的代码片段。

resolvable = false;

Resource resource = applicationContext.getResource(location);

if (resource != null && resource.exists()) {

resolvable = true;

}

为了验证解决方案的可行性,增加了一个新的html文件在 src/main/resources/templates/demo/world.html

3.2     方案一:定义多个 TemplateResolver

3.2.1            Custom TemplateResolver

Spring提供的实现类SpringResourceTemplateResolver,代码比较简单,我选择直接替换该类,而不是从它继承而来。

public class CodeStoryTemplateResolver extends AbstractConfigurableTemplateResolver

implements ApplicationContextAware {

private ApplicationContext applicationContext = null;

public CodeStoryTemplateResolver() {

super();

}

public void setApplicationContext(final ApplicationContext applicationContext)

throws BeansException {

this.applicationContext = applicationContext;

}

@Override

protected boolean computeResolvable(

IEngineConfiguration configuration, String ownerTemplate,

String template, Map<String, Object> templateResolutionAttributes) {

boolean resolvable = super.computeResolvable(configuration, ownerTemplate,

template, templateResolutionAttributes);

    if (resolvable) {

      // 判断文件是否存在

      resolvable = false;

      String pathName = getPrefix() + template + getSuffix();

      Resource resource = applicationContext.getResource(pathName);

      if (resource != null && resource.exists()) {

        resolvable = true;

      }

    }

return resolvable;

}

@Override

protected ITemplateResource computeTemplateResource(

final IEngineConfiguration configuration, final String ownerTemplate,

final String template, final String resourceName, final String characterEncoding,

final Map<String, Object> templateResolutionAttributes) {

return new SpringResourceTemplateResource(

this.applicationContext, resourceName, characterEncoding);

}

}

3.2.2            修改Spring配置

<bean id="webinfoTemplateResolver" class="....CodeStoryTemplateResolver">

<property name="prefix">

<value>/WEB-INF/</value>

</property>

<property name="suffix">

<value>.html</value>

</property>

<property name="templateMode">

<value>HTML</value>

</property>

<property name="characterEncoding">

<value>UTF-8</value>

</property>

<property name="cacheable" value="false"/>

</bean>

<bean id="webinfoTemplateResolver" class="....CodeStoryTemplateResolver">

<property name="prefix">

<value>classpath:/templates/</value>

</property>

<property name="suffix">

<value>.html</value>

</property>

<property name="templateMode">

<value>HTML</value>

</property>

<property name="characterEncoding">

<value>UTF-8</value>

</property>

<property name="cacheable" value="false"/>

</bean>

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">

<property name="templateResolvers">

<set>

<ref bean="webinfoTemplateResolver" />

<ref bean="classpathTemplateResolver" />

</set>

</property>

</bean>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">

<property name="templateEngine" ref="templateEngine" />

<property name="characterEncoding" value="UTF-8"/>

</bean>

重启系统测试,/demo/hello.html和/demo/world.html都能正常访问。

3.3     方案二:一个TemplateResolver支持prefixes

3.3.1            Custom TemplateResolver

理想的方案,是重载函数computeTemplateResource(),但这个函数被定义为final,无法重载,只好退而求其次选择重载computeResourceName()。在这个函数中判断是否定义了prefixes参数,如果是一次调用父类的computeResourceName()并判断资源是否存在。

public class CodeStoryTemplateResolver extends AbstractConfigurableTemplateResolver

implements ApplicationContextAware {

private ApplicationContext applicationContext = null;

public void setApplicationContext(final ApplicationContext applicationContext)

throws BeansException {

this.applicationContext = applicationContext;

}

  private String prefixes = null;

  public CodeStoryTemplateResolver() {

    super();

  }

  public final String getPrefixes() {

    return this.prefixes;

  }

  public final void setPrefixes(final String prefixes) {

    this.prefixes = prefixes;

  }

protected String computeResourceName(final IEngineConfiguration configuration,

final String ownerTemplate, final String template, final String prefix,

final String suffix, final boolean forceSuffix,

final Map<String, String> templateAliases,

final Map<String, Object> templateResolutionAttributes) {

String resourceName = null;

    String[] prefixes = null;

    if (!StringUtils.isEmptyOrWhitespace(getPrefixes())) {

      prefixes = getPrefixes().split(",");

    } else if (!StringUtils.isEmptyOrWhitespace(getPrefix())) {

      prefixes = new String[] { getPrefix() };

    } else {

      prefixes = new String[] { "" };

    }

    for (String onePrefix : prefixes) {

      onePrefix = StringUtil.trimLeft(StringUtil.trimRight(onePrefix));

      resourceName = super.computeResourceName(configuration, ownerTemplate,

             template, onePrefix, suffix,

        forceSuffix, templateAliases, templateResolutionAttributes);

      Resource resource = applicationContext.getResource(resourceName);

      if (resource != null && resource.exists()) {

        break;

      } else {

        resourceName = null;

      }

    }

return resourceName;

}

@Override

protected ITemplateResource computeTemplateResource(

final IEngineConfiguration configuration, final String ownerTemplate,

final String template, final String resourceName,final String characterEncoding,

final Map<String, Object> templateResolutionAttributes) {

return new SpringResourceTemplateResource(this.applicationContext,

resourceName, characterEncoding);

}

}

3.3.2            修改Spring配置

<bean id="multiTemplateResolver" class="....CodeStoryTemplateResolver">

<property name="prefixes">

<value>/WEB-INF/,classpath:/templates/</value>

</property>

<property name="suffix">

<value>.html</value>

</property>

<property name="templateMode">

<value>HTML</value>

</property>

<property name="characterEncoding">

<value>UTF-8</value>

</property>

<property name="cacheable" value="false"/>

</bean>

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">

<property name="templateResolvers">

<set>

<ref bean="multiTemplateResolver" />

</set>

</property>

</bean>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">

<property name="templateEngine" ref="templateEngine" />

<property name="characterEncoding" value="UTF-8"/>

</bean>

重启系统测试,/demo/hello.html和/demo/world.html都能正常访问。

3.4     啰嗦几句

两种方案区别不大,都是尝试增加文件判断,便于Thymeleaf找到真正的html所在路径。配置上,第二种方案相对简单一点。

性能方面,没有做仔细测试,估计比Spring缺省的TemplateResolver会慢一些。

当然,这个方案有点多次一举,最简单的处理方式,把目录src/main/webapps/WEB-INF/htmls 移动到 src/main/resources/templates即可。

Thymeleaf引擎支持Multi Prefix的更多相关文章

  1. spring mvc中添加对Thymeleaf的支持

    一.下载Thymeleaf 官方下载地址:https://dl.bintray.com/thymeleaf/downloads/thymeleaf/ 我下载的是最新的3.0.11版本 把包里的jar包 ...

  2. Atitti 存储引擎支持的国内点与特性attilax总结

    Atitti 存储引擎支持的国内点与特性attilax总结 存储引擎处理的事情: · 并发性:某些应用程序比其他应用程序具有很多的颗粒级锁定要求(如行级锁定). · 事务支持:并非所有的应用程序都需要 ...

  3. FBReader阅读引擎支持的功能

    "三十年河东,三十年河西"是一句民间谚语,它的来源是:从前黄河河道不固定,经常会改道(历史上无数次发生).某个地方原来在河的东面,若干年后,因黄河水流改道,这个地方会变为在河的西面 ...

  4. (转)支持Multi Range Read索引优化

    支持Multi Range Read索引优化 原文:http://book.51cto.com/art/201701/529465.htm http://book.51cto.com/art/2016 ...

  5. MyISAM、InnoDB、Memory这3个常用引擎支持的索引类型

    表格对比了MyISAM.InnoDB.Memory这3个常用引擎支持的索引类型: 索引 MyISAM引擎 InnoDB引擎 Memory引擎 B-Tree索引 支持 支持 支持 HASH索引 不支持 ...

  6. springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据

    springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...

  7. Flutter 多引擎支持 PlatformView 以及线程合并解决方案

    作者:字节移动技术-李皓骅 摘要 本文介绍了 Flutter 多引擎下,使用 PlatformView 场景时不能绕开的一个线程合并问题,以及它最终的解决方案.最终 Pull Request 已经 m ...

  8. Spring Boot thymeleaf模版支持,css,js等静态文件添加

    Thymeleaf引入 Thymeleaf是一个Java模板引擎开发库,可以处理和生成HTML.XML.JavaScript.CSS和文本,在Web和非Web环境下都可以正常工作. 1.添加依赖包 & ...

  9. SpringBoot入门篇--Thymeleaf引擎模板的基本使用方法

    我们在使用SpringBoot框架的时候在前面已经介绍了Thymelea引擎模板,因为SpringBoot对JSP惨不忍睹的支持.那我们在使用引擎模板对前端页面进行渲染能够返回的情况下我们怎么才能在静 ...

随机推荐

  1. h5前端流行的框架

    很多时候别人问你,上手的框架有哪些,其实我们都是知道的,只是一时却也说不上哪些比较,这里想给大家介绍一下,我所遇到的,还算好用的框架,做个分享 1 Bootstrap 官网:http://getboo ...

  2. Less运算和函数

    Less运算和函数   Less运算 在我们的 CSS 中,充斥着大量数值型的 value,比如 color.padding.margin 等.在某些情况下,这些数值之间是有着一定关系的,那么我们怎样 ...

  3. UEditor1.4.3.3实现图片上传、删除功能

    1.下载ueditor1.4.3.3 UTF-8的版本 2.新建一个项目,在项目中添加UEditor,把下载好的插件都放在ueditor这个文件夹中,在进行一些基本的配置 3.在根目录下新建一个为in ...

  4. 使用sklearn进行数据挖掘-房价预测(5)—训练模型

    使用sklearn进行数据挖掘系列文章: 1.使用sklearn进行数据挖掘-房价预测(1) 2.使用sklearn进行数据挖掘-房价预测(2)-划分测试集 3.使用sklearn进行数据挖掘-房价预 ...

  5. 学习笔记:UITabBarController使用详解

    一.手动创建UITabBarController 最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLa ...

  6. LoadRunner性能测试过程中报Error(-17998):Failed to get [param not passed in call] thread TLS entry.

    最近与其他公司一起合作使用loadrunner11进行性能测试,在此过程中,遇到Error(-17998)的错误,从网上搜索,找到的答案基本上都是说没有定义事务,但检查所有测试代码,发现都已经定义了事 ...

  7. uva 1418 - WonderTeam

    题意:你n支球队进行比赛,每两支队伍之间进行2场比赛,胜得3分,平得1分,输得0分,比赛后挑选出一个梦之队,要求进球总数最多,胜利场数最多,失球总数最少,并且三种都不能与其它对比列第一.问说梦之队的最 ...

  8. 前端测试框架Jest系列教程 -- 简介

    写在前面: 随着互联网日新月异的发展,用户对于页面的美观度,流畅度以及各方面的体验有了更高的要求,我们的网页不再是简单的承载文字,图片等简单的信息传递给用户,我们需要的是更加美观的页面展示,更快的浏览 ...

  9. file-loader 使用心得

    将webpack 里面的图片文件都放在制定文件夹. 配置如下 { test: /\.png$/, loader: "file-loader?name=imgs/[name]-[hash].[ ...

  10. UWP 五星好评

    var pfn = Package.Current.Id.FamilyName; await Launcher.LaunchUriAsync(new Uri("ms-windows-stor ...