No SpringMVC, tipos como data, dupla são definidos em feijão. Se nenhum processamento for feito, a data e o dobro não poderão ser vinculados.
A solução é usar a etiqueta @initbinder fornecida pelo Spring MVC
No meu projeto, adiciono o método initbinder no Basecontroller e uso a anotação da anotação @initbinder. Em seguida, o Spring MVC registrará esses editores antes de vincular o formulário. Obviamente, se você não se preocupar, também pode escrevê -los separadamente em cada um de seus controladores. Os controladores restantes herdam esta classe. A própria Spring fornece um grande número de classes de implementação, como CustomDateEditor, CustomBooleaDitor, CustomNumberEDitor etc., que são basicamente suficientes.
Obviamente, também não podemos usar essas aulas de editor que acompanham as nossas. Vamos construir alguns deles nós mesmos.
importar org.springframework.beans.propertyeditors.propertiesEditor; public class Doubleeditor estende o PropertiesEditor {@Override public void SeterStesext (texto da String) lança ilegalargumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (duplo.parseDouble (texto)); } @Override public string getastext () {return getValue (). Tostring (); }} importar org.springframework.beans.propertyeditors.propertiesEditor; classe pública INTEGEREDITOR estende o PropertiesEditor {@Override public void SeterStesext (text String) lança ilegalargumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (Integer.ParseInt (texto)); } @Override public string getastext () {return getValue (). Tostring (); }} importar org.springframework.beans.propertyeditors.propertiesEditor; public class Floateditor estende o PropertiesEditor {@Override public void SeterStesext (texto da string) lança ilegalargumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (float.parsefloat (text)); } @Override public string getastext () {return getValue (). Tostring (); }} importar org.springframework.beans.propertyeditors.propertiesEditor; public class Longeditor estende o PropertiesEditor {@Override public void SeterStesext (texto da String) lança ilegalargumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (long.parselong (texto)); } @Override public string getastext () {return getValue (). Tostring (); }}No BasController
@Initbinder protegido void initbinder (WebDatabinder Binder) {Binder.RegisterCustomeditor (DATE.Class, novo CustomDateEditor (new SimpleDateFormat ("AA YYYY-MM-DD HH: MM: SS"), True)); / Binder.RegisterCustomeditor (int.class, novo CustomNumberEDitor (int.class, true)); Binder.RegisterCustomeditor (int.class, new Integereditor ()); / Binder.RegisterCustomeditor (Long.class, novo CustomNumbereditor (Long.class, True)); Binder.RegisterCustomeditor (Long.class, new Longeditor ()); Binder.RegisterCustomeditor (Double.class, new Doubleeditor ()); Binder.RegisterCustomeditor (float.class, new Floateditor ()); } A cópia do código é a seguinte:
Classe pública org.springframework.beans.propertyeditors.propertiesEditor estende java.beans.propertyeditorsupport {
Ver? Também está ok se a sua classe de editor herdar diretamente a PropertyEditorSupport.
O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.