{"id":1701,"date":"2017-07-28T11:22:15","date_gmt":"2017-07-28T03:22:15","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1701"},"modified":"2017-07-28T11:22:15","modified_gmt":"2017-07-28T03:22:15","slug":"%e4%bd%bf%e7%94%a8springmvc%e4%b8%ad%e7%9a%84propertyeditor%e5%ae%9e%e7%8e%b0%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e5%92%8c%e6%95%b0%e6%8d%ae%e9%aa%8c%e8%af%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1701","title":{"rendered":"\u4f7f\u7528SpringMVC\u4e2d\u7684PropertyEditor\u5b9e\u73b0\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u548c\u6570\u636e\u9a8c\u8bc1"},"content":{"rendered":"<p>\n\t\u524d\u9762\u5173\u4e8eController\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u5b8c\u4e86\u3002\u4eca\u5929\u6211\u5c06\u5206\u4eab\u4e00\u4e0b\u5173\u4e8eSpringMVC\u4e2d\u7684\u6570\u636e\u8f6c\u6362\uff0c\u6570\u636e\u7ed1\u5b9a\u548c\u6570\u636e\u9a8c\u8bc1\u3002\n<\/p>\n<p>\n\t\u5148\u6765\u770b\u4e00\u5f20SpringMVC\u5904\u7406\u6570\u636e\u8bf7\u6c42\u7684\u6d41\u7a0b\u56fe\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"PropertyEditor\" src=\"http:\/\/ocuzluml6.bkt.clouddn.com\/springmvc__1.JPG\" style=\"width: 832px;height: 332px\" \/>\n<\/p>\n<p>\n\t\u4e0a\u56fe\u4e2d\u7684\u6d41\u7a0b\u8bf4\u660e\uff1a\n<\/p>\n<ol>\n<li>\n\t\t\u9996\u5148\u521b\u5efa\u6570\u636e\u7ed1\u5b9a\u5668\uff0c\u5728\u6b64\u6b64\u4f1a\u521b\u5efaServletRequestDataBinder\u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6emessageCodesResolver\uff08\u9519\u8bef\u7801\u89e3\u6790\u5668\uff09\uff1b\n\t<\/li>\n<li>\n\t\t\u63d0\u4f9b\u7b2c\u4e00\u4e2a\u6269\u5c55\u70b9\uff0c\u521d\u59cb\u5316\u6570\u636e\u7ed1\u5b9a\u5668\uff0c\u5728\u6b64\u5904\u6211\u4eec\u53ef\u4ee5\u8986\u76d6\u8be5\u65b9\u6cd5\u6ce8\u518c\u81ea\u5b9a\u4e49\u7684PropertyEditor\uff08\u8bf7\u6c42\u53c2\u6570&mdash;&mdash;&gt;\u547d\u4ee4\u5bf9\u8c61\u5c5e\u6027\u7684\u8f6c\u6362\uff09\uff1b\n\t<\/li>\n<li>\n\t\t\u8fdb\u884c\u6570\u636e\u7ed1\u5b9a\uff0c\u5373\u8bf7\u6c42\u53c2\u6570&mdash;&mdash;&gt;\u547d\u4ee4\u5bf9\u8c61\u7684\u7ed1\u5b9a\uff1b\n\t<\/li>\n<li>\n\t\t\u63d0\u4f9b\u7b2c\u4e8c\u4e2a\u6269\u5c55\u70b9\uff0c\u6570\u636e\u7ed1\u5b9a\u5b8c\u6210\u540e\u7684\u6269\u5c55\u70b9\uff0c\u6b64\u5904\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u7ed1\u5b9a\u52a8\u4f5c\uff1b\n\t<\/li>\n<li>\n\t\t\u9a8c\u8bc1\u5668\u5bf9\u8c61\u7684\u9a8c\u8bc1\uff0c\u9a8c\u8bc1\u5668\u901a\u8fc7validators\u6ce8\u5165\uff0c\u5982\u679c\u9a8c\u8bc1\u5931\u8d25\uff0c\u9700\u8981\u628a\u9519\u8bef\u4fe1\u606f\u653e\u5165Errors\uff08\u6b64\u5904\u4f7f\u7528BindException\u5b9e\u73b0\uff09\uff1b\n\t<\/li>\n<li>\n\t\t\u63d0\u4f9b\u7b2c\u4e09\u4e2a\u6269\u5c55\u70b9\uff0c\u6b64\u5904\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u7ed1\u5b9a\/\u9a8c\u8bc1\u903b\u8f91\uff1b\n\t<\/li>\n<li>\n\t\t\u5c06errors\u4f20\u5165\u529f\u80fd\u5904\u7406\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\uff0c\u529f\u80fd\u65b9\u6cd5\u5e94\u8be5\u5224\u65ad\u8be5\u9519\u8bef\u5bf9\u8c61\u662f\u5426\u6709\u9519\u8bef\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\n\t<\/li>\n<\/ol>\n<h2>\n\t\u6570\u636e\u7c7b\u578b\u8f6c\u6362<br \/>\n<\/h2>\n<p>\n\t\u8bf7\u6c42\u53c2\u6570\uff08String\uff09&mdash;&mdash;&gt;\u547d\u4ee4\u5bf9\u8c61\u5c5e\u6027\uff08\u53ef\u80fd\u662f\u4efb\u610f\u7c7b\u578b\uff09\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u5373\u6570\u636e\u7ed1\u5b9a\u65f6\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u4f7f\u7528PropertyEditor\u5b9e\u73b0\u7ed1\u5b9a\u65f6\u7684\u7c7b\u578b\u8f6c\u6362\u3002\n<\/p>\n<p>\n\tSpring\u5185\u5efa\u7684PropertyEditor\u5982\u4e0b\u6240\u793a\uff1a\n<\/p>\n<table border=\"1\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<p>\n\t\t\t\t\t<strong>\u7c7b\u540d<\/strong>\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\t<strong>\u8bf4\u660e<\/strong>\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t<strong>\u9ed8\u8ba4\u662f\u5426\u6ce8\u518c<\/strong>\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tByteArrayPropertyEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;byte[]\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tClassEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;Class\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\u5f53\u7c7b\u6ca1\u6709\u53d1\u73b0\u629b\u51fa<code>IllegalArgumentException<\/code>\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tCustomBooleanEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;Boolean\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\ttrue\/yes\/on\/1\u8f6c\u6362\u4e3atrue\uff0cfalse\/no\/off\/0\u8f6c\u6362\u4e3afalse\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tCustomCollectionEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\t\u6570\u7ec4\/Collection&mdash;&mdash;&gt;Collection\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\u666e\u901a\u503c&mdash;&mdash;&gt;Collection\uff08\u53ea\u5305\u542b\u4e00\u4e2a\u5bf9\u8c61\uff09\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\u5982String&mdash;&mdash;&gt;Collection\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\u4e0d\u5141\u8bb8Collection&mdash;&mdash;&gt;String\uff08\u5355\u65b9\u5411\u8f6c\u6362\uff09\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tCustomNumberEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;Number(Integer\u3001Long\u3001Double)\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tFileEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;File\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tInputStreamEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&mdash;&mdash;&gt;InputStream\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\u5355\u5411\u7684\uff0c\u4e0d\u80fdInputStream&mdash;&mdash;&gt;String\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tLocaleEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;Locale\uff0c\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\uff08String\u7684\u5f62\u5f0f\u4e3a[\u8bed\u8a00]_[\u56fd\u5bb6]_[\u53d8\u91cf]\uff0c\u8fd9\u4e0eLocal\u5bf9\u8c61\u7684toString()\u65b9\u6cd5\u5f97\u5230\u7684\u7ed3\u679c\u76f8\u540c\uff09\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tPatternEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;Pattern\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tPropertiesEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;java.lang.Properties\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tURLEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;URL\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&radic;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tStringTrimmerEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\t\u4e00\u4e2a\u7528\u4e8etrim \u7684 String\u7c7b\u578b\u7684\u5c5e\u6027\u7f16\u8f91\u5668\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\u5982\u9ed8\u8ba4\u5220\u9664\u4e24\u8fb9\u7684\u7a7a\u683c\uff0ccharsToDelete\u5c5e\u6027\uff1a\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u5176\u4ed6\u5b57\u7b26\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\temptyAsNull\u5c5e\u6027\uff1a\u5c06\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3anull\u503c\u7684\u9009\u9879\u3002\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&times;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tCustomDateEditor\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"422\">\n<p>\n\t\t\t\t\tString&lt;&mdash;&mdash;&gt;java.util.Date\n\t\t\t\t<\/p>\n<\/td>\n<td width=\"93\">\n<p>\n\t\t\t\t\t&times;\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n\tSpring\u5185\u5efa\u7684PropertyEditor\u652f\u6301\u7684\u5c5e\u6027\uff08\u7b26\u5408JavaBean\u89c4\u8303\uff09\u64cd\u4f5c\uff1a\n<\/p>\n<table border=\"1\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<p>\n\t\t\t\t\t<strong>\u8868\u8fbe\u5f0f<\/strong>\n\t\t\t\t<\/p>\n<\/td>\n<td>\n<p>\n\t\t\t\t\t<strong>\u8bbe\u503c\/\u53d6\u503c\u8bf4\u660e<\/strong>\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tusername\n\t\t\t\t<\/p>\n<\/td>\n<td>\n<p>\n\t\t\t\t\t\u5c5e\u6027username\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\u8bbe\u503c\u65b9\u6cd5setUsername()\/\u53d6\u503c\u65b9\u6cd5getUsername() \u6216 isUsername()\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tschooInfo.schoolType\n\t\t\t\t<\/p>\n<\/td>\n<td>\n<p>\n\t\t\t\t\t\u5c5e\u6027schooInfo\u7684\u5d4c\u5957\u5c5e\u6027schoolType\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\u8bbe\u503c\u65b9\u6cd5getSchooInfo().setSchoolType()\/\u53d6\u503c\u65b9\u6cd5getSchooInfo().getSchoolType()\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\thobbyList[0]\n\t\t\t\t<\/p>\n<\/td>\n<td>\n<p>\n\t\t\t\t\t\u5c5e\u6027hobbyList\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\t\u7d22\u5f15\u5c5e\u6027\u53ef\u80fd\u662f\u4e00\u4e2a\u6570\u7ec4\u3001\u5217\u8868\u3001\u5176\u5b83\u5929\u7136\u6709\u5e8f\u7684\u5bb9\u5668\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\n\t\t\t\t\tmap[key]\n\t\t\t\t<\/p>\n<\/td>\n<td>\n<p>\n\t\t\t\t\t\u5c5e\u6027map\uff08java.util.Map\u7c7b\u578b\uff09\n\t\t\t\t<\/p>\n<p>\n\t\t\t\t\tmap\u4e2dkey\u5bf9\u5e94\u7684\u503c\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n\t\u63a5\u4e0b\u6765\u6211\u4eec\u5199\u81ea\u5b9a\u4e49\u7684\u5c5e\u6027\u7f16\u8f91\u5668\u8fdb\u884c\u6570\u636e\u7ed1\u5b9a\u3002\u6a21\u578b\u5bf9\u8c61\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.chapter4.model;  \r\n\/\/\u7701\u7565import  \r\npublic class DataBinderTestModel {  \r\n    private String username;  \r\n    private boolean bool;\/\/Boolean\u503c\u6d4b\u8bd5  \r\n    private SchoolInfoModel schooInfo;  \r\n    private List hobbyList;\/\/\u96c6\u5408\u6d4b\u8bd5\uff0c\u6b64\u5904\u53ef\u4ee5\u6539\u4e3a\u6570\u7ec4\/Set\u8fdb\u884c\u6d4b\u8bd5  \r\n    private Map map;\/\/Map\u6d4b\u8bd5  \r\n    private PhoneNumberModel phoneNumber;\/\/String-&gt;\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u8f6c\u6362\u6d4b\u8bd5  \r\n    private Date date;\/\/\u65e5\u671f\u7c7b\u578b\u6d4b\u8bd5  \r\n    private UserState state;\/\/String&mdash;&mdash;&gt;Enum\u7c7b\u578b\u8f6c\u6362\u6d4b\u8bd5  \r\n    \/\/\u7701\u7565getter\/setter  \r\n}  \r\npackage com.xttblog.chapter4.model;  \r\n\/\/\u5982\u683c\u5f0f010-12345678  \r\npublic class PhoneNumberModel {  \r\n    private String areaCode;\/\/\u533a\u53f7  \r\n    private String phoneNumber;\/\/\u7535\u8bdd\u53f7\u7801  \r\n    \/\/\u7701\u7565getter\/setter  \r\n}  <\/pre>\n<p>\n\tPhoneNumber\u5c5e\u6027\u7f16\u8f91\u5668\n<\/p>\n<p>\n\t\u524d\u53f0\u8f93\u5165\u5982010-12345678\u81ea\u52a8\u8f6c\u6362\u4e3aPhoneNumberModel\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.chapter4.web.controller.support.editor;  \r\n\/\/\u7701\u7565import  \r\npublic class PhoneNumberEditor extends PropertyEditorSupport {  \r\n    Pattern pattern = Pattern.compile(&quot;^(\\\\d{3,4})-(\\\\d{7,8})$&quot;);  \r\n    @Override  \r\n    public void setAsText(String text) throws IllegalArgumentException {  \r\n        if(text == null || !StringUtils.hasLength(text)) {  \r\n            setValue(null); \/\/\u5982\u679c\u6ca1\u503c\uff0c\u8bbe\u503c\u4e3anull  \r\n        }  \r\n        Matcher matcher = pattern.matcher(text);  \r\n        if(matcher.matches()) {  \r\n            PhoneNumberModel phoneNumber = new PhoneNumberModel();  \r\n            phoneNumber.setAreaCode(matcher.group(1));  \r\n            phoneNumber.setPhoneNumber(matcher.group(2));  \r\n            setValue(phoneNumber);  \r\n        } else {  \r\n            throw new IllegalArgumentException(String.format(&quot;\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\uff0c\u9700\u8981\u683c\u5f0f[010-12345678]\uff0c\u4f46\u683c\u5f0f\u662f[%s]&quot;, text));  \r\n        }  \r\n    }  \r\n    @Override  \r\n    public String getAsText() {  \r\n        PhoneNumberModel phoneNumber = ((PhoneNumberModel)getValue());  \r\n        return phoneNumber == null ? &quot;&quot; : phoneNumber.getAreaCode() + &quot;-&quot; + phoneNumber.getPhoneNumber();  \r\n    }  \r\n}  <\/pre>\n<ul>\n<li>\n\t\tPropertyEditorSupport\uff1a\u4e00\u4e2aPropertyEditor\u7684\u652f\u6301\u7c7b\uff1b\n\t<\/li>\n<li>\n\t\tsetAsText\uff1a\u8868\u793a\u5c06String&mdash;&mdash;&gt;PhoneNumberModel\uff0c\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u8f6c\u6362\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\u629b\u51fa\u5f02\u5e38\uff0c\u5219\u63a5\u4e0b\u6765\u7684\u9a8c\u8bc1\u5668\u4f1a\u8fdb\u884c\u9a8c\u8bc1\u5904\u7406\uff1b\n\t<\/li>\n<li>\n\t\tgetAsText\uff1a\u8868\u793a\u5c06PhoneNumberModel&mdash;&mdash;&gt;String\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u9700\u8981\u5728\u63a7\u5236\u5668\u6ce8\u518c\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u5c5e\u6027\u7f16\u8f91\u5668\u3002<br \/>\n\t\u6b64\u5904\u6211\u4eec\u4f7f\u7528AbstractCommandController\uff0c\u56e0\u4e3a\u5b83\u7ee7\u627f\u4e86BaseCommandController\uff0c\u62e5\u6709\u7ed1\u5b9a\u6d41\u7a0b\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.chapter4.web.controller;  \r\n\/\/\u7701\u7565import  \r\npublic class DataBinderTestController extends AbstractCommandController {  \r\n    public DataBinderTestController() {  \r\n        setCommandClass(DataBinderTestModel.class); \/\/\u8bbe\u7f6e\u547d\u4ee4\u5bf9\u8c61  \r\n        setCommandName(&quot;dataBinderTest&quot;);\/\/\u8bbe\u7f6e\u547d\u4ee4\u5bf9\u8c61\u7684\u540d\u5b57  \r\n    }  \r\n    @Override  \r\n    protected ModelAndView handle(HttpServletRequest req, \r\n\t\tHttpServletResponse resp, Object command, \r\n\t\tBindException errors) throws Exception {  \r\n        \/\/\u8f93\u51facommand\u5bf9\u8c61\u770b\u770b\u662f\u5426\u7ed1\u5b9a\u6b63\u786e  \r\n        System.out.println(command);  \r\n        return new ModelAndView(&quot;bindAndValidate\/success&quot;).addObject(&quot;dataBinderTest&quot;, command);  \r\n    }  \r\n    @Override  \r\n    protected void initBinder(HttpServletRequest request, \r\n\t\tServletRequestDataBinder binder) throws Exception {  \r\n        super.initBinder(request, binder);  \r\n        \/\/\u6ce8\u518c\u81ea\u5b9a\u4e49\u7684\u5c5e\u6027\u7f16\u8f91\u5668  \r\n        \/\/1\u3001\u65e5\u671f  \r\n        DateFormat df = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);  \r\n        CustomDateEditor dateEditor = new CustomDateEditor(df, true);  \r\n        \/\/\u8868\u793a\u5982\u679c\u547d\u4ee4\u5bf9\u8c61\u6709Date\u7c7b\u578b\u7684\u5c5e\u6027\uff0c\u5c06\u4f7f\u7528\u8be5\u5c5e\u6027\u7f16\u8f91\u5668\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362  \r\n        binder.registerCustomEditor(Date.class, dateEditor);  \r\n        \/\/\u81ea\u5b9a\u4e49\u7684\u7535\u8bdd\u53f7\u7801\u7f16\u8f91\u5668  \r\n        binder.registerCustomEditor(PhoneNumberModel.class, new PhoneNumberEditor());  \r\n    }  \r\n}  <\/pre>\n<ul>\n<li>\n\t\tinitBinder:\u7b2c\u4e00\u4e2a\u6269\u5c55\u70b9\uff0c\u521d\u59cb\u5316\u6570\u636e\u7ed1\u5b9a\u5668\uff0c\u5728\u6b64\u5904\u6211\u4eec\u6ce8\u518c\u4e86\u4e24\u4e2a\u5c5e\u6027\u7f16\u8f91\u5668\uff1b\n\t<\/li>\n<li>\n\t\tCustomDateEditor\uff1a\u81ea\u5b9a\u4e49\u7684\u65e5\u671f\u7f16\u8f91\u5668\uff0c\u7528\u4e8e\u5728String&lt;&mdash;&mdash;&gt;\u65e5\u671f\u4e4b\u95f4\u8f6c\u6362\uff1b\n\t<\/li>\n<li>\n\t\tbinder.registerCustomEditor(Date.class, dateEditor)\uff1a\u8868\u793a\u5982\u679c\u547d\u4ee4\u5bf9\u8c61\u662fDate\u7c7b\u578b\uff0c\u5219\u4f7f\u7528dateEditor\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff1b\n\t<\/li>\n<li>\n\t\tPhoneNumberEditor\uff1a\u81ea\u5b9a\u4e49\u7684\u7535\u8bdd\u53f7\u7801\u5c5e\u6027\u7f16\u8f91\u5668\u7528\u4e8e\u5728String&lt;&mdash;&mdash;&gt; PhoneNumberModel\u4e4b\u95f4\u8f6c\u6362\uff1b\n\t<\/li>\n<li>\n\t\tbinder.registerCustomEditor(PhoneNumberModel.class, newPhoneNumberEditor())\uff1a\u8868\u793a\u5982\u679c\u547d\u4ee4\u5bf9\u8c61\u662fPhoneNumberModel\u7c7b\u578b\uff0c\u5219\u4f7f\u7528PhoneNumberEditor\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff1b\n\t<\/li>\n<\/ul>\n<p>\n\tspring\u914d\u7f6e\u6587\u4ef6chapter4-servlet.xml\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean name=&quot;\/dataBind&quot;   \r\nclass=&quot;com.xttblog.chapter4.web.controller.DataBinderTestController&quot;\/&gt;  <\/pre>\n<p>\n\t\u89c6\u56fe\u9875\u9762\uff08WEB-INF\/jsp\/bindAndValidate\/success.jsp\uff09\n<\/p>\n<pre class=\"brush:xml;\">\r\nEL phoneNumber:${dataBinderTest.phoneNumber}&lt;br\/&gt;  \r\nEL state:${dataBinderTest.state}&lt;br\/&gt;  \r\nEL date:${dataBinderTest.date}&lt;br\/&gt; <\/pre>\n<p>\n\t\u4e0b\u9762\u8fdb\u5165\u6d4b\u8bd5\u90e8\u5206\uff0c\u5728\u6d4f\u89c8\u5668\u5730\u5740\u680f\u8f93\u5165\u8bf7\u6c42\u7684URL\uff0c\u5982\uff1a\n<\/p>\n<p>\n\thttp:\/\/localhost:9080\/springmvc-chapter4\/dataBind?username=zhang&amp;bool=yes&amp;schooInfo.specialty=computer&amp;hobbyList[0]=program&amp;hobbyList[1]=music&amp;map[key1]=value1&amp;map[key2]=value2&amp;phoneNumber=010-12345678&amp;date=2012-3-18 16:48:48&amp;state=blocked\n<\/p>\n<p>\n\t\u63a7\u5236\u5668\u8f93\u51fa\u7684\u5185\u5bb9\uff1a\n<\/p>\n<p>\n\tDataBinderTestModel [username=zhang, bool=true, schooInfo=SchoolInfoModel [schoolType=null, schoolName=null, specialty=computer], hobbyList=[program, music], map={key1=value1, key2=value2}, phoneNumber=PhoneNumberModel [areaCode=010, phoneNumber=12345678], date=Sun Mar 18 16:48:48 CST 2012, state=\u9501\u5b9a]\n<\/p>\n<p>\n\t\u7c7b\u578b\u8f6c\u6362\u5982\u56fe\u6240\u793a\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u7c7b\u578b\u8f6c\u6362\" src=\"http:\/\/ocuzluml6.bkt.clouddn.com\/82fd547be3b3668828ab02d02f9298de__2.JPG\" style=\"width: 859px;height: 380px\" \/>\n<\/p>\n<h2>\n\t\u6ce8\u518cPropertyEditor<br \/>\n<\/h2>\n<p>\n\t\u4f7f\u7528WebDataBinder\u8fdb\u884c\u63a7\u5236\u5668\u7ea7\u522b\u6ce8\u518cPropertyEditor\uff08\u63a7\u5236\u5668\u72ec\u4eab\uff09\uff0c\u4f7f\u7528WebDataBinder\u6ce8\u518c\u63a7\u5236\u5668\u7ea7\u522b\u7684PropertyEditor\uff0c\u8fd9\u79cd\u65b9\u5f0f\u6ce8\u518c\u7684PropertyEditor\u53ea\u5bf9\u5f53\u524d\u63a7\u5236\u5668\u72ec\u4eab\uff0c\u5373\u5176\u4ed6\u7684\u63a7\u5236\u5668\u4e0d\u4f1a\u81ea\u52a8\u6ce8\u518c\u8fd9\u4e2aPropertyEditor\uff0c\u5982\u679c\u9700\u8981\u8fd8\u9700\u8981\u518d\u6ce8\u518c\u4e00\u4e0b\u3002\n<\/p>\n<p>\n\t\u4f7f\u7528WebBindingInitializer\u6279\u91cf\u6ce8\u518cPropertyEditor\uff0c\u5982\u679c\u60f3\u5728\u591a\u4e2a\u63a7\u5236\u5668\u540c\u65f6\u6ce8\u518c\u591a\u4e2a\u76f8\u540c\u7684PropertyEditor\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528WebBindingInitializer\u3002\n<\/p>\n<p>\n\t\u5b9e\u73b0WebBindingInitializer\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.chapter4.web.controller.support.initializer;  \r\n\/\/\u7701\u7565import  \r\npublic class MyWebBindingInitializer implements WebBindingInitializer {  \r\n    @Override  \r\n    public void initBinder(WebDataBinder binder, WebRequest request) {  \r\n        \/\/\u6ce8\u518c\u81ea\u5b9a\u4e49\u7684\u5c5e\u6027\u7f16\u8f91\u5668  \r\n        \/\/1\u3001\u65e5\u671f  \r\n        DateFormat df = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);  \r\n        CustomDateEditor dateEditor = new CustomDateEditor(df, true);  \r\n        \/\/\u8868\u793a\u5982\u679c\u547d\u4ee4\u5bf9\u8c61\u6709Date\u7c7b\u578b\u7684\u5c5e\u6027\uff0c\u5c06\u4f7f\u7528\u8be5\u5c5e\u6027\u7f16\u8f91\u5668\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362  \r\n        binder.registerCustomEditor(Date.class, dateEditor);  \r\n        \/\/\u81ea\u5b9a\u4e49\u7684\u7535\u8bdd\u53f7\u7801\u7f16\u8f91\u5668  \r\n        binder.registerCustomEditor(PhoneNumberModel.class, new PhoneNumberEditor());  \r\n    }  \r\n}  <\/pre>\n<p>\n\t\u901a\u8fc7\u5b9e\u73b0WebBindingInitializer\u5e76\u901a\u8fc7binder\u6ce8\u518c\u591a\u4e2aPropertyEditor\u3002\n<\/p>\n<p>\n\t\u4fee\u6539DataBinderTestController\uff0c\u6ce8\u91ca\u6389initBinder\u65b9\u6cd5\uff1b\n<\/p>\n<p>\n\t\u4fee\u6539chapter4-servlet.xml\u914d\u7f6e\u6587\u4ef6\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;!-- \u6ce8\u518cWebBindingInitializer\u5b9e\u73b0 --&gt;  \r\n&lt;bean id=&quot;myWebBindingInitializer&quot; class=&quot;com.xttblog.chapter4.web.controller.support.initializer.MyWebBindingInitializer&quot;\/&gt;  \r\n&lt;bean name=&quot;\/dataBind&quot; class=&quot;com.xttblog.chapter4.web.controller.DataBinderTestController&quot;&gt;  \r\n    &lt;!-- \u6ce8\u5165WebBindingInitializer\u5b9e\u73b0 --&gt;  \r\n    &lt;property name=&quot;webBindingInitializer&quot; ref=&quot;myWebBindingInitializer&quot;\/&gt;  \r\n&lt;\/bean&gt;<\/pre>\n<p>\n\t\u4f7f\u7528WebBindingInitializer\u7684\u597d\u5904\u662f\u5f53\u4f60\u9700\u8981\u5728\u591a\u4e2a\u63a7\u5236\u5668\u4e2d\u9700\u8981\u540c\u65f6\u4f7f\u7528\u591a\u4e2a\u76f8\u540c\u7684PropertyEditor\u53ef\u4ee5\u5728WebBindingInitializer\u5b9e\u73b0\u4e2d\u6ce8\u518c\uff0c\u8fd9\u6837\u53ea\u9700\u8981\u5728\u63a7\u5236\u5668\u4e2d\u6ce8\u5165WebBindingInitializer\u5373\u53ef\u6ce8\u5165\u591a\u4e2aPropertyEditor\u3002\n<\/p>\n<h2>\n\t\u5168\u5c40\u7ea7\u522b\u6ce8\u518cPropertyEditor\uff08\u5168\u5c40\u5171\u4eab\uff09<br \/>\n<\/h2>\n<p>\n\t\u53ea\u9700\u8981\u5c06\u6211\u4eec\u81ea\u5b9a\u4e49\u7684PropertyEditor\u653e\u5728\u548c\u4f60\u7684\u6a21\u578b\u7c7b\u540c\u5305\u4e0b\u5373\u53ef\uff0c\u4e14\u4f60\u7684Editor\u547d\u540d\u89c4\u5219\u5fc5\u987b\u662f&ldquo;\u6a21\u578b\u7c7b\u540dEditor&rdquo;\uff0c\u8fd9\u6837Spring\u4f1a\u81ea\u52a8\u4f7f\u7528\u6807\u51c6JavaBean\u67b6\u6784\u8fdb\u884c\u81ea\u52a8\u8bc6\u522b\uff0c\u5982\u56fe\u6240\u793a\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u5168\u5c40\u7ea7\u522b\u6ce8\u518cPropertyEditor\uff08\u5168\u5c40\u5171\u4eab\uff09\" src=\"http:\/\/ocuzluml6.bkt.clouddn.com\/5d5c8cd2c8e42c5413849ac090f4d145__3.JPG\" style=\"width: 414px;height: 242px\" \/>\n<\/p>\n<p>\n\t\u6b64\u65f6\u6211\u4eec\u628a&ldquo;DataBinderTestController&rdquo;\u7684&ldquo;binder.registerCustomEditor(PhoneNumberModel.class, new PhoneNumberEditor());&rdquo;\u6ce8\u91ca\u6389\uff0c\u518d\u5c1d\u8bd5\u8bbf\u95ee\u4e0a\u9762\u5b9e\u4f8b\u4e2d\u7684\u6d4b\u8bd5URL\u5373\u53ef\u6210\u529f\u3002\n<\/p>\n<p>\n\t\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u4ec5\u5728\u4f7f\u7528Spring\u65f6\u53ef\u7528\uff0c\u5728\u6807\u51c6\u7684JavaBean\u7b49\u73af\u5883\u90fd\u662f\u53ef\u7528\u7684\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u662f\u5168\u5c40\u5171\u4eab\u7684\uff08\u4e0d\u4ec5\u4ec5\u662fSpring\u73af\u5883\uff09\u3002\n<\/p>\n<p>\n\tPropertyEditor\u88ab\u9650\u5236\u4e3a\u53ea\u80fdString&lt;&mdash;&mdash;&gt;Object\u4e4b\u95f4\u8f6c\u6362\uff0c\u4e0d\u80fdObject&lt;&mdash;&mdash;&gt;Object\uff0cSpring3\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u7c7b\u578b\u8f6c\u6362\uff08TypeConversion\uff09\u652f\u6301\uff0c\u5b83\u53ef\u4ee5\u5728\u4efb\u610f\u5bf9\u8c61\u4e4b\u95f4\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u4e0d\u4ec5\u4ec5\u662fString&lt;&mdash;&mdash;&gt;Object\u3002<br \/>\n\t&nbsp;<br \/>\n\t\u5982\u679c\u6211\u5728\u5730\u5740\u680f\u8f93\u5165\u9519\u8bef\u7684\u6570\u636e\uff0c\u5373\u6570\u636e\u7ed1\u5b9a\u5931\u8d25\uff0cSpring Web MVC\u8be5\u5982\u4f55\u5904\u7406\u5462\uff1f\u5982\u679c\u6211\u8f93\u5165\u7684\u6570\u636e\u4e0d\u5408\u6cd5\u5462\uff1f\u5982\u7528\u6237\u540d\u8f93\u5165100\u4e2a\u5b57\u7b26\uff08\u8d85\u957f\u4e86\uff09\u90a3\u53c8\u8be5\u600e\u4e48\u5904\u7406\u5462\uff1f\u51fa\u9519\u4e86\u9700\u8981\u9519\u8bef\u6d88\u606f\uff0c\u90a3\u9519\u8bef\u6d88\u606f\u5e94\u8be5\u662f\u786c\u7f16\u7801\uff1f\u8fd8\u662f\u53ef\u914d\u7f6e\u5462\uff1f<br \/>\n\t&nbsp;<br \/>\n\t\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u5b66\u4e60\u4e00\u4e0b\u6570\u636e\u9a8c\u8bc1\u5668\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u5427\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1701\">\u4f7f\u7528SpringMVC\u4e2d\u7684PropertyEditor\u5b9e\u73b0\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u548c\u6570\u636e\u9a8c\u8bc1<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u524d\u9762\u5173\u4e8eController\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u5b8c\u4e86\u3002\u4eca\u5929\u6211\u5c06\u5206\u4eab\u4e00\u4e0b\u5173\u4e8eSpringMVC\u4e2d\u7684\u6570\u636e\u8f6c\u6362\uff0c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,1359,413],"class_list":["post-1701","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-spring-mvc-","tag-springmvc"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1701"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1701\/revisions"}],"predecessor-version":[{"id":1702,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1701\/revisions\/1702"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}