في springMVC ، يتم تعريف أنواع مثل التاريخ ، المزدوج في الفاصوليا. إذا لم يتم إجراء معالجة ، لا يمكن ربط التاريخ والمضاعفة.
الحل هو استخدام علامة initBinder التي توفرها SPRING MVC
في مشروعي ، أقوم بإضافة طريقة initbinder في BaseController واستخدم شرح التعليق التوضيحي @initbinder. ثم ستقوم Spring MVC بتسجيل هؤلاء المحررين قبل ربط النموذج. بالطبع ، إذا كنت لا تهتم ، فيمكنك أيضًا كتابتها بشكل منفصل في كل من وحدات التحكم الخاصة بك. ترث وحدات التحكم المتبقية هذه الفئة. يوفر Spring نفسه عددًا كبيرًا من فئات التنفيذ ، مثل CustomDateEditor و CustomBooleAneditor و CustomNumbereditor ، وما إلى ذلك ، والتي هي في الأساس بما فيه الكفاية.
بالطبع ، لا يمكننا أيضًا استخدام فصول المحرر هذه التي تأتي معنا. دعونا نبني عددًا قليلًا منهم.
استيراد org.springframework.beans.PropertyEditors.Propertieseditor ؛ يمتد DoubleDitor الفئة العامة Propertieseditor {Override public void setastext (نص سلسلة) يلقي alfortleargumentException {if (text == null || text.equals ("")) {text = "0" ؛ } setValue (double.parsedouble (text)) ؛ } Override public string getastext () {return getValue (). toString () ؛ }} استيراد org.springframework.beans.PropertyEditors.Propertieseditor ؛ الطبقة العامة integereditor يمتد propertieseditor {Override public void setastext (نص سلسلة) يلقي alfortalargumentException {if (text == null || text.equals ("")) {text = "0" ؛ } setValue (integer.parseint (text)) ؛ } Override public string getastext () {return getValue (). toString () ؛ }} استيراد org.springframework.beans.PropertyEditors.Propertieseditor ؛ الطبقة العامة floateditor يمتد propertieseditor {Override public void setastext (نص سلسلة) يلقي alfortalargumentException {if (text == null || text.equals ("")) {text = "0" ؛ } setValue (float.parsefloat (text)) ؛ } Override public string getastext () {return getValue (). toString () ؛ }} استيراد org.springframework.beans.PropertyEditors.Propertieseditor ؛ يمتد فئة Public Class Stareditor Propertieseditor {Override public void setastext (نص سلسلة) يلقي alfortalargumentException {if (text == null || text.equals ("")) {text = "0" ؛ } setValue (long.parselong (text)) ؛ } Override public string getastext () {return getValue (). toString () ؛ }}في Basecontroller
initBinder proted void initbinder (WebDatabinder Binder) {binder.registerCustomeditor (date.class ، customDateEditor new (new SimpledAteFormat ("Yyyy-MM-DD HH: MM: SS") ، true)) ؛ / binder.registerCustomEditor (int.class ، customnumbereditor new (int.class ، true)) ؛ Binder.registerCustomEditor (int.class ، new Integereditor ()) ؛ / binder.registerCustomeditor (long.class ، customnumbereditor new (long.class ، true)) ؛ Binder.registerCustomeditor (Long.Class ، New Stareditor ()) ؛ Binder.registerCustomeditor (double.class ، new Doubleeditor ()) ؛ Binder.registerCustomeditor (float.class ، new floateditor ()) ؛ } نسخة الكود كما يلي:
الطبقة العامة org.springframework.beans.propertyEditors.Propertieseditor يمتد java.beans.propertyeditorsupport {
يرى؟ لا بأس أيضًا إذا كانت فئة المحرر الخاصة بك ترث مباشرة propertyeditorsupport.
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.