SpringMVCでは、日付などのタイプは豆で定義されています。処理が行われない場合、日付とダブルをバインドできません。
解決策は、Spring MVCが提供する@initbinderタグを使用することです
私のプロジェクトでは、BasecontrollerにMethod initbinderを追加し、Annotation @initbinder Annotationを使用します。次に、Spring MVCはフォームをバインドする前にこれらの編集者を登録します。もちろん、気にしない場合は、各コントローラーに個別に書き込むこともできます。残りのコントローラーはこのクラスを継承します。 Spring自体は、CustomDateEditor、CustomBooleAneditor、CustomNumberEditorなど、基本的に十分な多くの実装クラスを提供します。
もちろん、私たち自身の編集者クラスを使用することもできません。それらのいくつかを自分で構築しましょう。
Import org.springframework.beans.propertyeditors.propertieseditor;パブリッククラスのダブルエディターはPropertiesEditorを拡張します{@Override public void setastext(string text)throws IllegalargumentException {if(text == null || text.equals( "")){text = "0"; } setValue(double.parsedouble(text)); } @Override public string getastext(){retun getValue()。toString(); }} Import org.springframework.beans.propertyeditors.propertieseditor; public class integereditorはPropertieseditorを拡張します{@Override public void setastext(string text)throws IllegalargumentException {if(text == null || text.equals( "")){text = "0"; } setValue(integer.parseint(text)); } @Override public string getastext(){retun getValue()。toString(); }} Import org.springframework.beans.propertyeditors.propertieseditor; Public class floateditorはPropertieseditorを拡張します{@Override public void setastext(string text)throws IllegalargumentException {if(text == null || text.equals( "")){text = "0"; } setValue(float.parsefloat(text)); } @Override public string getastext(){retun getValue()。toString(); }} Import org.springframework.beans.propertyeditors.propertieseditor; Public Class LongeditorはPropertiesEditorを拡張します{@Override public void setastext(string text)throws IllegalargumentException {if(text == null || text.equals( "")){text = "0"; } setValue(long.parselong(text)); } @Override public string getastext(){retun getValue()。toString(); }}Basecontrollerで
@initbinder Protected void initbinder(webdatabinder binder){binder.registercustomeditor(date.class、new CustomDateEditor(new SimpledateFormat( "yyyy-mm-dd hh:mm:ss")、true)); / binder.registercustomeditor(int.class、new CustomNumbumberEditor(int.class、true)); Binder.registerCustomeditor(int.class、new Integereditor()); / binder.registercustomeditor(long.class、new CustomNumberEditor(long.class、true)); binder.registercustomeditor(long.class、new longeditor()); binder.registercustomeditor(double.class、new DoubleEditor()); Binder.registerCustomeditor(float.class、new floateditor()); }コードコピーは次のとおりです。
パブリッククラスorg.springframework.beans.propertyeditors.propertieseditorはjava.beans.propertyeditorsupportを拡張します{
見る?また、エディタークラスがPropertyEditorsupportを直接継承しても問題ありません。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。