Using Spring's MVC, you directly bind page parameters to an object, and an error occurs when a property in the object has a date, which needs to be handled at this time.
Similarly, other types that need to be processed can be used in this way.
Add code to Controller
@InitBinder
protected void Initbinder (HttpServletRequest request,
Servletrequestdatabinder binder) throws Exception {
//for properties that need to be converted to Date types, dateeditor is used for processing
binder.registercustomeditor (Date.class, New Dateeditor ()) ;
}
Dateeditor is inherited from PropertyEditorSupport for a custom processing class, and the processing method is public void Setastext (String text) throws IllegalArgumentException
Import Org.springframework.util.StringUtils;
Import Java.beans.PropertyEditorSupport;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date; public class Dateeditor extends PropertyEditorSupport {private static final dateformat DateFormat = new Simpledatefo
Rmat ("Yyyy-mm-dd");
private static final DateFormat TimeFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Private DateFormat DateFormat;
Private Boolean allowempty = true;
Public Dateeditor () {} public dateeditor (DateFormat dateformat) {this.dateformat = DateFormat;
Public Dateeditor (DateFormat DateFormat, Boolean allowempty) {This.dateformat = DateFormat;
This.allowempty = AllowEmpty;
}/** * Parse the Date from the given text, using the specified DateFormat. */@Override public void Setastext (String text) throws IllegalArgumentException {if (this.allowempty &&!
Stringutils.hastext (text) {//Treat empty String as null value.
SetValue (NULL); else {try {if (This.dateformat!= null) setValue (This.dateFormat.parse (
text));
else {if (Text.contains (":")) SetValue (Timeformat.parse (text));
Else SetValue (dateformat.parse (text)); } catch (ParseException ex) {throw new IllegalArgumentException ("Could not parse Date:" +
Ex.getmessage (), ex);
}}/** * Format the Date as String, using the specified DateFormat.
*/@Override public String Getastext () {Date value = (date) getValue ();
DateFormat DateFormat = This.dateformat;
if (DateFormat = = null) DateFormat = TimeFormat; return (value!= null Dateformat.format (value): "");
}
}