Spirng自定義註解(驗證身份證+性別+地區)
阿新 • • 發佈:2018-11-30
第一步:定義註解
MyDateTimeFormat:
package com.wbg.maven1128.intface; import java.lang.annotation.*; @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE,ElementType.TYPE}) public @interface MyDateTimeFormat { }View Code
第二步:建立實體類
Person:
package com.wbg.maven1128.entity; import com.wbg.maven1128.intface.MyDateTimeFormat; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; public class Person { public Person getPerson() { return person; }View Codepublic void setPerson(Person person) { this.person = person; } @MyDateTimeFormat private Person person; String sid; public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } String province; public Person(String sid, String province, Date birthday, String sex) {this.sid = sid; this.province = province; this.birthday = birthday; this.sex = sex; } public Person(String province, Date birthday, String sex) { this.province = province; this.birthday = birthday; this.sex = sex; } public Person() { } @Override public String toString() { return "省份:"+this.getProvince()+"出生日期:"+this.getBirthday()+"性別:"+this.getSex(); } public String getProvince() { return province; } public void setProvince(String province) { this.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; } @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday; String sex; }
第三步:建立實現類呼叫介面Formatter<Person>
MyDateFormatter:
package com.wbg.maven1128.intface; import com.wbg.maven1128.entity.Person; import org.springframework.format.Formatter; import java.text.ParseException; import java.util.*; public class MyDateFormatter implements Formatter<Person> { @Override public String print(Person object, Locale locale) { return null; } @Override public Person parse(String text, Locale locale) throws ParseException { if(text.length()!=18){ throw new ParseException("請輸入18位身份證",59); } else if(text.length()==18&&Verification(text)){ return getPerson(text); }else { throw new ParseException("身份證輸入錯誤",59); } } Person getPerson(String text){ Person person=new Person(); Calendar instance = Calendar.getInstance(); int year=Integer.parseInt(text.substring(6,10)); int month=Integer.parseInt(text.substring(10,12)); int date=Integer.parseInt(text.substring(12,14)); instance.set(year,month,date); person.setBirthday(instance.getTime()); person.setProvince(provinces.get(Integer.parseInt(text.substring(0,2)))); person.setSex(Integer.parseInt(text.substring(16,17))%2==0?"女":"男"); return person; } static Map<Integer,String> provinces = new HashMap<Integer, String>(){{ this.put(11,"北京市"); this.put(12,"天津市"); this.put(13,"河北省"); this.put(14,"山西省"); this.put(15,"內蒙古自治區"); this.put(21,"遼寧省"); this.put(22,"吉林省"); this.put(23,"黑龍江省"); this.put(31,"上海市"); this.put(32,"江蘇省"); this.put(33,"浙江省"); this.put(34,"安徽省"); this.put(35,"福建省"); this.put(36,"江西省"); this.put(37,"山東省"); this.put(41,"河南省"); this.put(42,"湖北省"); this.put(43,"湖南省"); this.put(44,"廣東省"); this.put(45,"廣西壯族自治區"); this.put(46,"海南省"); this.put(51,"四川省"); this.put(52,"貴州省"); this.put(53,"雲南省"); this.put(54,"西藏自治區"); this.put(50,"重慶市"); this.put(61,"陝西省"); this.put(62,"甘肅省"); this.put(63,"青海省"); this.put(64,"寧夏回族自治區"); this.put(65,"新疆維吾爾族自治區"); this.put(83,"臺灣地區"); this.put(81,"香港特別行政區"); this.put(82,"澳門特別行政區"); }}; boolean Verification(String text){ String []aa={"7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2"}; String []bb={"1","0","X ","9","8","7","6","5","4","3","2"}; int sum=0; for (int i = 0; i < 17; i++) { sum+=Integer.parseInt(text.substring(i, 1 + i))*Integer.parseInt(aa[i]); } return bb[sum%11].equals(text.substring(17,18)); } }View Code
第四步:呼叫
MyDataTimeFormatAnnotationFormatterFactory介面AnnotationFormatterFactory<MyDateFormatter>
package com.wbg.maven1128.intface; import com.wbg.maven1128.entity.Person; import org.springframework.context.support.EmbeddedValueResolutionSupport; import org.springframework.format.AnnotationFormatterFactory; import org.springframework.format.Formatter; import org.springframework.format.Parser; import org.springframework.format.Printer; import java.util.*; public class MyDataTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport implements AnnotationFormatterFactory<MyDateTimeFormat> { private static final Set<Class<?>> FIELD_TYPES; static { Set<Class<?>> fieldTypes = new HashSet<Class<?>>(4); //新增可以使用註解的型別 fieldTypes.add(String.class); fieldTypes.add(Person.class); FIELD_TYPES = Collections.unmodifiableSet(fieldTypes); } @Override public Set<Class<?>> getFieldTypes() { return FIELD_TYPES; } @Override public Printer<?> getPrinter(MyDateTimeFormat annotation, Class<?> fieldType) { return getFormatter(annotation, fieldType); } @Override public Parser<?> getParser(MyDateTimeFormat annotation, Class<?> fieldType) { return getFormatter(annotation, fieldType); } protected Formatter<Person> getFormatter(MyDateTimeFormat annotation, Class<?> fieldType) { MyDateFormatter formatter = new MyDateFormatter(); return formatter; } }View Code
第四步:啟用註解
<mvc:default-servlet-handler /> <bean name="factoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatters"> <set> <bean class="com.wbg.maven1128.intface.MyDataTimeFormatAnnotationFormatterFactory" > </bean> </set> </property> </bean> <mvc:annotation-driven conversion-service="factoryBean"/>View Code
jsp頁面:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2018/11/28 Time: 16:44 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form method="post" action="/tagadd"> <p> order:<input type="text" name="person"/> </p> <input type="submit"/> </form> </body> </html>View Code
controller
package com.wbg.maven1128.controller; import com.wbg.maven1128.entity.Person; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/person") public class PersonController { @RequestMapping(value = "/get",produces = "application/json;charset=utf-8") @ResponseBody public String getPerson(Person person){ try { return person.getPerson().toString(); }catch (Exception e){ System.out.println(e.getMessage()); return e.getMessage(); } } @GetMapping public String index(){ return "person_index"; } }View Code
最後直接測試