Dans SpringMVC, des types tels que la date, le double sont définis dans les haricots. Si aucun traitement n'est effectué, la date et le double ne peuvent pas être liés.
La solution consiste à utiliser la balise @initbinder fournie par Spring MVC
Dans mon projet, j'ajoute la méthode initbinder dans BaseController et j'utilise l'annotation annotation @initbinder. Ensuite, Spring MVC enregistrera ces éditeurs avant de lier le formulaire. Bien sûr, si vous ne vous inquiétez pas, vous pouvez également les écrire séparément dans chacun de vos contrôleurs. Les contrôleurs restants héritent de cette classe. Spring lui-même fournit un grand nombre de classes d'implémentation, telles que CustomDateEditor, CustomBooleanEdiator, CustomNumberEditor, etc., qui sont fondamentalement suffisantes.
Bien sûr, nous ne pouvons pas non plus utiliser ces classes d'éditeurs qui viennent avec les nôtres. Construisons nous-mêmes quelques-uns.
import org.springframework.beans.propertyeditors.propertiesEditor; La classe publique Doubleditor étend PropertiesEditor {@Override public void setastext (String text) lève illégalArgumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (double.parsedouble (texte)); } @Override public String getastext () {return getValue (). ToString (); }} import org.springframework.beans.propertyeditors.propertiesEditor; classe publique IntegeReditor étend PropertiesEditor {@Override public void setastext (String text) lève illégalArgumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (Integer.ParseInt (texte)); } @Override public String getastext () {return getValue (). ToString (); }} import org.springframework.beans.propertyeditors.propertiesEditor; classe publique Floateditor étend PropertiesEditor {@Override public void setastext (String text) lève illégalArgumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (float.parsefloat (texte)); } @Override public String getastext () {return getValue (). ToString (); }} import org.springframework.beans.propertyeditors.propertiesEditor; La classe publique LongEditor étend PropertiesEditor {@Override public void setastext (String text) lève illégalArgumentException {if (text == null || text.equals ("")) {text = "0"; } setValue (long.parselong (texte)); } @Override public String getastext () {return 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 CustomNumberEditor (int.class, true)); Binder.RegisterCustomEditor (int.class, new IntegeRiditor ()); / Binder.RegisterCustomEditor (long.class, new CustomNumberEditor (long.class, true)); Binder.RegisterCustomEditor (long.class, new LongEditor ()); Binder.RegisterCustomEditor (double.class, new Doubleditor ()); Binder.RegisterCustomEditor (float.class, new Floateditor ()); } La copie de code est la suivante:
classe publique org.springframework.beans.propertyeditors.propertiesEditor étend java.beans.propertyeditorsupport {
Voir? Il est également OK si votre classe d'éditeur hérite directement de PropertyEditOrSupport.
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.