{"id":1726,"date":"2017-08-02T14:46:07","date_gmt":"2017-08-02T06:46:07","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1726"},"modified":"2017-08-02T14:46:07","modified_gmt":"2017-08-02T06:46:07","slug":"%e8%af%a6%e8%a7%a3springmvc%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae%e9%aa%8c%e8%af%81%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1726","title":{"rendered":"\u8be6\u89e3SpringMVC\u4e2d\u7684\u6570\u636e\u9a8c\u8bc1\u793a\u4f8b"},"content":{"rendered":"<p>\n\t\u5728\u300a<a href=\"http:\/\/www.xttblog.com\/?p=1724\" name=\"\u8be6\u89e3SpringMVC\u4e2d\u7684Errors\u548cBindingResult\u6570\u636e\u9a8c\u8bc1\" target=\"_blank\" title=\"\u8be6\u89e3SpringMVC\u4e2d\u7684Errors\u548cBindingResult\u6570\u636e\u9a8c\u8bc1\">\u8be6\u89e3SpringMVC\u4e2d\u7684Errors\u548cBindingResult\u6570\u636e\u9a8c\u8bc1<\/a>\u300b\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u4e00\u4e2a\u6848\u4f8b\uff0c\u672c\u6587\u5c06\u7ee7\u7eed\u5b66\u4e60\u4e00\u4e2a\u6570\u636e\u7ed1\u5b9a\u5931\u8d25\u7684\u6848\u4f8b\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u5148\u770b\u770b\u63a7\u5236\u5668DataBinderErrorTestController\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.chapter4.web.controller;  \r\n\/\/\u7701\u7565import  \r\npublic class DataBinderErrorTestController extends SimpleFormController {  \r\n    public DataBinderErrorTestController() {  \r\n        setCommandClass(DataBinderTestModel.class);  \r\n        setCommandName(&quot;dataBinderTest&quot;);  \r\n    }  \r\n    @Override  \r\n    protected ModelAndView showForm(HttpServletRequest request, \r\n\t\tHttpServletResponse response, BindException errors) throws Exception {  \r\n              \/\/\u5982\u679c\u8868\u5355\u63d0\u4ea4\u6709\u4efb\u4f55\u9519\u8bef\u90fd\u4f1a\u518d\u56de\u5230\u8868\u5355\u5c55\u793a\u9875\u9762  \r\n              System.out.println(errors);  \r\n              return super.showForm(request, response, errors);  \r\n    }  \r\n    @Override  \r\n    protected void doSubmitAction(Object command) throws Exception {  \r\n        System.out.println(command); \/\/\u8868\u5355\u63d0\u4ea4\u6210\u529f\uff08\u6570\u636e\u7ed1\u5b9a\u6210\u529f\uff09\u8fdb\u884c\u529f\u80fd\u5904\u7406  \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\t  \/\/\u6ce8\u518c\u81ea\u5b9a\u4e49\u7684\u5c5e\u6027\u7f16\u8f91\u5668  \r\n\t  \/\/1\u3001\u65e5\u671f  \r\n\t  DateFormat df = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);  \r\n\t  CustomDateEditor dateEditor = new CustomDateEditor(df, true);  \r\n\t  \/\/\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\t  binder.registerCustomEditor(Date.class, dateEditor);  \r\n\t   \r\n\t  \/\/\u81ea\u5b9a\u4e49\u7684\u7535\u8bdd\u53f7\u7801\u7f16\u8f91\u5668  \r\n\t  binder.registerCustomEditor(PhoneNumberModel.class, new PhoneNumberEditor());  \r\n    }  \r\n}  <\/pre>\n<p>\n\t\u6b64\u5904\u6211\u4eec\u4f7f\u7528SimpleFormController\uff1a\n<\/p>\n<ul>\n<li>\n\t\tshowForm\uff1a\u5c55\u793a\u8868\u5355\uff0c\u5f53\u63d0\u4ea4\u8868\u5355\u6709\u4efb\u4f55\u6570\u636e\u7ed1\u5b9a\u9519\u8bef\u4f1a\u518d\u56de\u5230\u8be5\u65b9\u6cd5\u8fdb\u884c\u8868\u5355\u8f93\u5165\uff08\u5728\u6b64\u5904\u6211\u4eec\u6253\u5370\u9519\u8bef\u5bf9\u8c61\uff09\uff1b\n\t<\/li>\n<li>\n\t\tdoSubmitAction\uff1a\u8868\u5355\u63d0\u4ea4\u6210\u529f\uff0c\u53ea\u8981\u5f53\u8868\u5355\u7684\u6570\u636e\u5230\u547d\u4ee4\u5bf9\u8c61\u7ed1\u5b9a\u6210\u529f\u65f6\uff0c\u624d\u4f1a\u6267\u884c\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;\/dataBindError&quot;  \r\nclass=&quot;com.xttblog.web.controller.DataBinderErrorTestController&quot;&gt;  \r\n   &lt;property name=&quot;formView&quot; value=&quot;bindAndValidate\/input&quot;\/&gt;  \r\n   &lt;property name=&quot;successView&quot; value=&quot;bindAndValidate\/success&quot;\/&gt;  \r\n&lt;\/bean&gt;  <\/pre>\n<p>\n\t\u89c6\u56fe\u9875\u9762\uff08WEB-INF\/jsp\/bindAndValidate\/input.jsp\uff09\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;%@ page language=&quot;java&quot; contentType=&quot;text\/html; charset=UTF-8&quot; pageEncoding=&quot;UTF-8&quot;%&gt;  \r\n&lt;%@taglib prefix=&quot;form&quot; uri=&quot;http:\/\/www.springframework.org\/tags\/form&quot; %&gt;  \r\n&lt;!-- \u8868\u5355\u7684\u547d\u4ee4\u5bf9\u8c61\u540d\u4e3adataBinderTest --&gt;  \r\n&lt;form:form commandName=&quot;dataBinderTest&quot;&gt;  \r\n    &lt;form:errors path=&quot;*&quot; cssStyle=&quot;color:red&quot;&gt;&lt;\/form:errors&gt;&lt;br\/&gt;&lt;br\/&gt;  \r\n    bool:&lt;form:input path=&quot;bool&quot;\/&gt;&lt;br\/&gt;  \r\n    phoneNumber:&lt;form:input path=&quot;phoneNumber&quot;\/&gt;&lt;br\/&gt;  \r\n    date:&lt;form:input path=&quot;date&quot;\/&gt;&lt;br\/&gt;  \r\n    &lt;input type=&quot;submit&quot; value=&quot;\u63d0\u4ea4&quot;\/&gt;  \r\n&lt;\/form:form&gt;<\/pre>\n<p>\n\t\u6b64\u5904\u4e00\u5b9a\u8981\u4f7f\u7528form\u6807\u7b7e\u5e93\uff0c\u501f\u6b64\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83\u7684\u5f3a\u5927\u652f\u6301\uff08\u522b\u7684\u5927\u90e8\u5206Web\u6846\u67b6\u6240\u4e0d\u5177\u5907\u7684\uff0c\u5c55\u793a\u7528\u6237\u9a8c\u8bc1\u5931\u8d25\u7684\u6570\u636e\uff09\u3002\n<\/p>\n<ul>\n<li>\n\t\t&lt;form:form commandName=&quot;dataBinderTest&quot;&gt;\uff1a\u6307\u5b9a\u547d\u4ee4\u5bf9\u8c61\u4e3adataBinderTest\uff0c\u9ed8\u8ba4command\uff1b\n\t<\/li>\n<li>\n\t\t&lt;form:errors path=&quot;*&quot; cssStyle=&quot;color:red&quot;&gt;&lt;\/form:errors&gt;\uff1a\u663e\u793a\u9519\u8bef\u6d88\u606f\uff0c\u5f53\u63d0\u4ea4\u8868\u5355\u6709\u9519\u8bef\u65f6\u5c55\u793a\u9519\u8bef\u6d88\u606f\uff08\u6570\u636e\u7ed1\u5b9a\u9519\u8bef\/\u6570\u636e\u4e0d\u5408\u6cd5\uff09\uff1b\n\t<\/li>\n<li>\n\t\t&lt;form:input path=&quot;bool&quot;\/&gt;\uff1a\u7b49\u4ef7\u4e8e\uff08&lt;input type=&rsquo;text&rsquo;&gt;\uff09\uff0c\u4f46\u4f1a\u4ece\u547d\u4ee4\u5bf9\u8c61\u4e2d\u53d6\u51fabool\u5c5e\u6027\u8fdb\u884c\u586b\u5145value\u5c5e\u6027\uff0c\u6216\u5982\u679c\u8868\u5355\u63d0\u4ea4\u6709\u9519\u8bef\u4f1a\u4ece\u9519\u8bef\u5bf9\u8c61\u53d6\u51fa\u4e4b\u524d\u7684\u9519\u8bef\u6570\u636e\uff08\u800c\u975e\u7a7a\u6216\u9ed8\u8ba4\u503c\uff09\uff1b\n\t<\/li>\n<li>\n\t\t&lt;input type=&quot;submit&quot; value=&quot;\u63d0\u4ea4&quot;\/&gt;\uff1aspring\u6ca1\u6709\u63d0\u4f9b\u76f8\u5e94\u7684\u63d0\u4ea4\u6309\u94ae\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528html\u7684\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u5728\u5730\u5740\u680f\u8f93\u5165\u5982\u4e0b\u5730\u5740\uff1ahttp:\/\/localhost:9080\/springmvc-chapter4\/dataBindError\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"dataBindError\" src=\"http:\/\/oeb6uoqqa.bkt.clouddn.com\/20170802004.JPG\" style=\"width: 299px;height: 121px\" \/>\n<\/p>\n<p>\n\t\u5168\u90e8\u662f\u9519\u8bef\u6570\u636e\uff0c\u5373\u4e0d\u80fd\u7ed1\u5b9a\u5230\u6211\u4eec\u7684\u547d\u4ee4\u5bf9\u8c61\uff1b\u5f53\u63d0\u4ea4\u8868\u5355\u540e\uff0c\u6211\u4eec\u53c8\u56de\u5230\u8868\u5355\u8f93\u5165\u9875\u9762\uff0c\u800c\u4e14\u8f93\u51fa\u4e86\u4e00\u5806\u9519\u8bef\u4fe1\u606f\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u9519\u8bef\u4fe1\u606f\" src=\"http:\/\/oeb6uoqqa.bkt.clouddn.com\/20170802005.JPG\" style=\"width: 860px;height: 311px\" \/>\n<\/p>\n<ul>\n<li>\n\t\t\u9519\u8bef\u6d88\u606f\u4e0d\u53ef\u8bfb\uff1b\n\t<\/li>\n<li>\n\t\t\u8868\u5355\u5143\u7d20\u53ef\u4ee5\u663e\u793a\u4e4b\u524d\u7684\u9519\u8bef\u7684\u6570\u636e\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u503c\/\u7a7a\uff1b\n\t<\/li>\n<\/ul>\n<p>\n\t\u8fd9\u91cc\u6700\u5927\u7684\u95ee\u9898\u662f\u4e0d\u53ef\u8bfb\u7684\u9519\u8bef\u6d88\u606f\uff0c\u5982\u4f55\u8ba9\u8fd9\u4e9b\u9519\u8bef\u6d88\u606f\u53ef\u8bfb\u5462\uff1f\n<\/p>\n<p>\n\t\u9996\u5148\u6211\u4eec\u770b\u6211\u4eec\u7684showForm\u65b9\u6cd5\u91cc\u8f93\u51fa\u7684&ldquo;errors&rdquo;\u9519\u8bef\u5bf9\u8c61\u4fe1\u606f\uff1a\n<\/p>\n<pre class=\"brush:plain;\">\r\norg.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 3 errors  \r\nField error in object &#039;dataBinderTest&#039; on field &#039;bool&#039;: rejected value [www]; codes [typeMismatch.dataBinderTest.bool,typeMismatch.bool,typeMismatch.boolean,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [dataBinderTest.bool,bool]; arguments []; default message [bool]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'boolean' for property 'bool'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [www]]  \r\nField error in object &#039;dataBinderTest&#039; on field &#039;date&#039;: rejected value [123]; codes [typeMismatch.dataBinderTest.date,typeMismatch.date,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [dataBinderTest.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: \"123\"]  \r\nField error in object &#039;dataBinderTest&#039; on field &#039;phoneNumber&#039;: rejected value [123]; codes [typeMismatch.dataBinderTest.phoneNumber,typeMismatch.phoneNumber,typeMismatch.cn.javass.chapter4.model.PhoneNumberModel,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [dataBinderTest.phoneNumber,phoneNumber]; arguments []; default message [phoneNumber]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'cn.javass.chapter4.model.PhoneNumberModel' for property 'phoneNumber'; nested exception is java.lang.IllegalArgumentException: \u7c7b\u578b\u8f6c\u6362\u5931\u8d25\uff0c\u9700\u8981\u683c\u5f0f[010-12345678]\uff0c\u4f46\u683c\u5f0f\u662f[123]]  <\/pre>\n<p>\n\t\u6570\u636e\u7ed1\u5b9a\u5931\u8d25\uff08\u7c7b\u578b\u4e0d\u5339\u914d\uff09\u4f1a\u81ea\u52a8\u751f\u6210\u5982\u4e0b\u9519\u8bef\u7801\uff08\u9519\u8bef\u7801\u5bf9\u5e94\u7684\u9519\u8bef\u6d88\u606f\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u4f9d\u6b21\u67e5\u627e\uff09\uff1a\n<\/p>\n<ul>\n<li>\n\t\ttypeMismatch.\u547d\u4ee4\u5bf9\u8c61\u540d.\u5c5e\u6027\u540d\n\t<\/li>\n<li>\n\t\ttypeMismatch.\u5c5e\u6027\u540d\n\t<\/li>\n<li>\n\t\ttypeMismatch.\u5c5e\u6027\u5168\u9650\u5b9a\u7c7b\u540d\uff08\u5305\u540d.\u7c7b\u540d\uff09\n\t<\/li>\n<li>\n\t\ttypeMismatch\n\t<\/li>\n<\/ul>\n<p>\n\t\u5185\u90e8\u4f7f\u7528MessageCodesResolver\u89e3\u6790\u6570\u636e\u7ed1\u5b9a\u9519\u8bef\u5230\u9519\u8bef\u7801\uff0c\u9ed8\u8ba4DefaultMessageCodesResolver\uff0c\u56e0\u6b64\u60f3\u8981\u8be6\u7ec6\u4e86\u89e3\u5982\u4f55\u89e3\u6790\u8bf7\u770b\u5176javadoc\uff1b\u5efa\u8bae\u4f7f\u7528\u7b2c1\u4e2a\u8fdb\u884c\u9519\u8bef\u7801\u7684\u914d\u7f6e\u3002\n<\/p>\n<p>\n\t\u56e0\u6b64\u4fee\u6539\u6211\u4eec\u7684messages.properties\u6dfb\u52a0\u5982\u4e0b\u9519\u8bef\u6d88\u606f\uff08\u9700\u8981\u6267\u884cNativeToAscii\uff09\uff1a\n<\/p>\n<pre class=\"brush:plain;\">\r\ntypeMismatch.dataBinderTest.date=\u60a8\u8f93\u5165\u7684\u6570\u636e\u683c\u5f0f\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff08\u683c\u5f0f\uff1a2012-03-19 22:17:17\uff09  \r\n#typeMismatch.date=2  \r\n#typeMismatch.java.util.Date=3  \r\n#typeMismatch=4  <\/pre>\n<p>\n\t\u518d\u6b21\u63d0\u4ea4\u8868\u5355\u6211\u4eec\u4f1a\u770b\u5230\u6211\u4eec\u8bbe\u7f6e\u7684\u9519\u8bef\u6d88\u606f\u3002\u5230\u6b64\uff0c\u6570\u636e\u7ed1\u5b9a\u9519\u8bef\u6211\u4eec\u4ecb\u7ecd\u5b8c\u4e86\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u518d\u770b\u4e00\u4e0b\u6570\u636e\u4e0d\u5408\u6cd5\u9519\u8bef\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=1726\">\u8be6\u89e3SpringMVC\u4e2d\u7684\u6570\u636e\u9a8c\u8bc1\u793a\u4f8b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5728\u300a\u8be6\u89e3SpringMVC\u4e2d\u7684Errors\u548cBindingResult\u6570\u636e\u9a8c\u8bc1\u300b\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u4e00\u4e2a [&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":[1359,413],"class_list":["post-1726","post","type-post","status-publish","format-standard","hentry","category-java","tag-spring-mvc-","tag-springmvc"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1726","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=1726"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1726\/revisions"}],"predecessor-version":[{"id":1727,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1726\/revisions\/1727"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}