Thymeleaf从入门到专家-第四篇The Conversion Service转换服务

1、configuration

As explained before, Thymeleaf can make use of a Conversion Service registered at the Application Context. Our application configuration class, by extending Spring’s own WebMvcConfigurerAdapter helper, will automatically register such conversion service, which we can configure by adding the formatters that we need. Let’s see again what it looks like:

如前所述,Thymeleaf可以使用在应用程序上下文中注册的转换服务。我们的应用程序配置类通过扩展Spring自己的WebMvcConfigurerAdapter帮助程序,将自动注册这样的转换服务,我们可以通过添加我们需要的格式化程序来配置它。让我们再看一下它的样子:

官方示例

@Override
public void addFormatters(final FormatterRegistry registry) {
    super.addFormatters(registry);
    registry.addFormatter(varietyFormatter());
    registry.addFormatter(dateFormatter());
}

@Bean
public VarietyFormatter varietyFormatter() {
    return new VarietyFormatter();
}

@Bean
public DateFormatter dateFormatter() {
    return new DateFormatter();
}

示例

编写转换器

/**
 * @Description: 转换器 {@link Formatter}
 * @Author: vesus
 * @CreateDate: 2018/12/27 下午4:33
 * @Version: 1.0
 */
public class MyDateFormatter implements Formatter<Date> {

    public Date parse(String s, Locale locale) throws ParseException {
        return new SimpleDateFormat("yyyy-mm-dd").parse(s);
    }

    public String print(Date o, Locale locale) {
        return new SimpleDateFormat("dd-mm-yyyy").format(o);
    }
}

编写适配器

/**
 * @Description: mvc 配置类适配器
 * @Author: vesus
 * @CreateDate: 2018/12/27 下午4:29
 * @Version: 1.0
 */
@Configuration
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        super.addFormatters(registry);
        registry.addFormatter(dateFormatter());
    }

    @Bean
    MyDateFormatter dateFormatter(){
        return new MyDateFormatter();
    }
}

2、Double-brace syntax 双括号语法

The Conversion Service can be easily applied in order to convert/format any object into String. This is done by means of the double-brace expression syntax:

  • For variable expressions: ${{…}}
  • For selection expressions: *{{…}}

So, for example, given an Integer-to-String converter that adds commas as a thousands separator, this:

可以轻松应用转换服务,以便将任何对象转换/格式化为String。这是通过双括号表达式语法完成的:

  • 对于变量表达式:$ {{…}}
  • 对于选择表达式:* {{…}}

因此,例如,给定一个Integer-to-String转换器,将逗号添加为千位分隔符,这样

<p th:text="${val}">...</p>
<p th:text="${{val}}">...</p>

<p>1234567890</p>
<p>1,234,567,890</p>

3、Use in forms 在表单中使用

We saw before that every th:field attribute will always apply the conversion service, so this:

我们之前看到,每个th:field属性将始终应用转换服务,因此:

<input type="text" th:field="*{datePlanted}" />

等同于

<input type="text" th:field="*{{datePlanted}}" />

4、 #conversions utility object

The #conversions expression utility object allows the manual execution of the Conversion Service wherever needed:

conversions表达式实用程序对象允许在需要时手动执行转换服务

<p th:text="${'Val: ' + #conversions.convert(val,'String')}">...</p>

Syntax for this utility object:

  • #conversions.convert(Object,Class): converts the object to the specified class.
  • #conversions.convert(Object,String): same as above, but specifying the target class as a String (note the java.lang. package can be ommitted).

此实用程序对象的语法:

  • #conversions.convert(Object,Class):将对象转换为指定的类。
  • #conversions.convert(Object,String):与上面相同,但是将目标类指定为String(注意可以省略java.lang包)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值