Spring MVC 基于AnnotationFormatterFactory接口实现自定义的规则
1、创建一个类来实现AnnotationFormatterFactory接口
代码:
package com.oukele.CustomAnnotation; import com.oukele.model.CardId; import org.springframework.format.AnnotationFormatterFactory; import org.springframework.format.Parser; import org.springframework.format.Printer; import java.util.HashSet; import java.util.Set; public class MyFormatterAnnotation implements AnnotationFormatterFactory<FormId> { @Override public Set<Class<?>> getFieldTypes() { HashSet<Class<?>> types = new HashSet<>(); types.add(CardId.class); return types; } @Override public Printer<?> getPrinter(FormId annotation, Class<?> fieldType) { return null; } @Override public Parser<?> getParser(FormId annotation, Class<?> fieldType) { return getFormatter(annotation); } private MyFormatter getFormatter(FormId annotation){ MyFormatter myFormatter = new MyFormatter(); //获取注解的值,并赋值 myFormatter.setLanguage(annotation.value()); return myFormatter; } }
通过实现Formatter接口自定义自已的规则
代码:
package com.oukele.CustomAnnotation; import com.oukele.model.CardId; import org.springframework.format.Formatter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class MyFormatter implements Formatter<CardId> { //字段 private String language; // 输出 @Override public String print(CardId object, Locale locale) { return null; } // 转换 @Override public CardId parse(String text, Locale locale) throws ParseException { //获取传入的身份证号 //然后截取对应的数字,给CardId的省份、出生日期、性别 赋值 return new CardId(getProvince(text),getBirth(text),getSex(text)); } private String getProvince(String car){ return "暂无数据"; } private Date getBirth(String car) throws ParseException { return new SimpleDateFormat("yyyyMMdd").parse(car.substring(6, 14)); } private String getSex(String car) { if( language == null){ // 17: 偶数 女,奇数 男 return car.charAt(16) % 2 == 0 ? "女" : "男"; }else{ return car.charAt(16) % 2 == 0 ? "grill" : "boy"; } } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } }
如何实现自定义的规则(Formatter)的地址:https://www.cnblogs.com/oukele/p/10042607.html
FormId注解代码:
package com.oukele.CustomAnnotation; import java.lang.annotation.*; @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface FormId { String value() default ""; }
CardId类的代码:
package com.oukele.model; import java.util.Date; public class CardId { //身份证 private String info; //省份 private String Province; //出生日期 private Date birthday; //性别 private String sex; public CardId(String province, Date birthday, String sex) { Province = province; this.birthday = birthday; this.sex = sex; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getProvince() { return Province; } public void setProvince(String province) { Province = province; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
Controller层代码:
注意:要使用自定义的类型转换器要在(比如 在spring-web配置文件 )注册,并启用。
注册:
启用:
运行结果:
浏览器显示的结果:
↓↓↓↓↓↓↓↓↓↓↓↓↓↓
有多努力就有多幸运。