Java 前端模板引擎学习:thymeleaf 模板引擎

模板引擎接口 ITemplateEngine

一、后台数据与外部数据

1.处理后台数据

$表达式是个变量表达式,用于处理在  request parameters and the request, session and application 中的变量

  • ${x} will return a variable x stored into the Thymeleaf context or as a request attribute.
  • ${param.x} will return a request parameter called x (which might be multivalued).
  • ${session.x} will return a session attribute called x.
  • ${application.x} will return a servlet context attribute called x.
<input type="text" name="userName" value="James Carrot" th:value="${user.name}" />

  

2.处理外部数据

外部化的片段通常叫作 messages,#表达式用于处理这类消息。外部消息可以从数据库中获取,或从 .properties files 中获取,这取决于 StandardMessageResolver 的实现。Thymeleaf 的默认实现为 StandardMessageResolver。

<p th:text="#{home.welcome}">Welcome to our grocery store!</p>

为了实现属性名的  i18n,消息解析器 StandardMessageResolver 将/WEB-INF/templates/home.html 映射于同文件夹同名文 propreties 上,比如

  • /WEB-INF/templates/home_en.properties for English texts.
  • /WEB-INF/templates/home_es.properties for Spanish language texts.
  • /WEB-INF/templates/home_pt_BR.properties for Portuguese (Brazil) language texts.
  • /WEB-INF/templates/home.properties for default texts (if the locale is not matched).

二、文本数据处理

扫描二维码关注公众号,回复: 3330212 查看本文章

1.非编码

如果 $-expression 获取的值包含 < 等html实体符号,默认会对其进行实体编码

比如:

home.welcome=Welcome to our <b>fantastic</b> grocery store!

使用 th:text  返回

<p th:text="#{home.welcome}">Welcome to our grocery store!</p>

<p>Welcome to our <b>fantastic</b> grocery store!</p>

使用 th:utext  返回

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

<p>Welcome to our <b>fantastic</b> grocery store!</p>

  

猜你喜欢

转载自www.cnblogs.com/lemos/p/9696751.html