En SpringMVC, los tipos como la fecha, el doble se definen en frijoles. Si no se realiza ningún procesamiento, la fecha y el doble no pueden estar obligados.
La solución es usar la etiqueta @InitBinder proporcionada por Spring MVC
En mi proyecto, agrego el método InitBinder en Basecontroller y uso la anotación de anotación @InitBinder. Luego, Spring MVC registrará estos editores antes de vincular el formulario. Por supuesto, si no se molesta, también puede escribirlos por separado en cada uno de sus controladores. Los controladores restantes heredan esta clase. Spring en sí mismo proporciona una gran cantidad de clases de implementación, como CustomDateEditor, CustomBooleanEditor, CustomNumberEditor, etc., que básicamente son suficientes.
Por supuesto, tampoco podemos usar estas clases de editor que vienen con las nuestras. Construyamos algunos de ellos nosotros mismos.
importar org.springframework.beans.propertyEditors.propertiesEditor; La clase pública DoubleEditor extiende PropertiesEditor {@Override public void setastext (texto de cadena) arroja ilegalargumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (double.parsedubo (text)); } @Override public String getastext () {return getValue (). ToString (); }} importar org.springframework.beans.propertyEditors.propertiesEditor; Public Class IntegerEditor extiende PropertiesEditor {@Override public void setastext (texto de cadena) arroja 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 extiende PropertiesEditor {@Override public void setastext (texto de cadena) arroja ilegalargumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (float.parsefloat (texto)); } @Override public String getastext () {return getValue (). ToString (); }} importar org.springframework.beans.propertyEditors.propertiesEditor; La clase pública Longeditor extiende PropertiesEditor {@Override public void setastext (texto de cadena) arroja ilegalargumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (long.parselong (texto)); } @Override public String getastext () {return getValue (). ToString (); }}En basecontrolador
@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 CustomNumberEditor (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 ()); } La copia del código es la siguiente:
Public Class org.springframework.beans.propertyEditors.propertiesEditor extiende java.beans.propertyEditorsupport {
¿Ver? También está bien si la clase de su editor hereda directamente PropertyEditorsupport.
Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.