Converter
函数式接口
能够将任意类型转换为指定的任意类型:
S是源类型,T是目标类型。
比如字符串->日期
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
public class DateConverter implements Converter{
private String pattern = "yyyy-MM-dd HH:mm:ss,s";
@Override
public Date convert(String arg0) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try{
return simpleDateFormat.parse(arg0);
} catch(ParseException parseException) {
throw new IllegalArgumentException("this pattern"+pattern);
}
}
}
配置文件添加配置
页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
converter Date
controller
@RequestMapping("/toDate")
public String toDate(){
return "date";
}
@RequestMapping("/getDate")
public String converterDate(Date date,Model model){
model.addAttribute("message", date);
return "first";
}