轉(zhuǎn)換器與格式化

2018-09-30 15:43 更新

轉(zhuǎn)換器與格式化

轉(zhuǎn)換器:converter
格式化:Format

在“校驗(yàn)器”、“文件上傳”中的例子中都使用了數(shù)據(jù)綁定(將表單數(shù)據(jù)綁定到bean對(duì)象中),例如:

@RequestMapping(value = "/output")
public String output(Person person, Model model) {
    model.addAttribute("person", person);
    return "hello/output";
}

表單的數(shù)據(jù)都是String類型,如果我們的bean類中的屬性是其他類型,例如Date、int,這時(shí)候就需要寫一個(gè)工具,將String轉(zhuǎn)換成Date、int。
這就是轉(zhuǎn)換器與格式化做的事情:類型轉(zhuǎn)換。字符串轉(zhuǎn)換成數(shù)字類型是內(nèi)置的。

資料:
9. Validation, Data Binding, and Type Conversion
Spring MVC request parameter conversion with minimal configuration
Introduction to Spring Converters and Formatters

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)