1. Annotation method, implemented initBinder annotation at the controller layer
@InitBinderpublic void initBinder(HttpServletRequest request,ServletRequestDataBinder binder)throws Exception { DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); CustomDateEditor dateEditor = new CustomDateEditor(fmt, true); binder.registerCustomEditor(Date.class, dateEditor); }2. Type conversion, SpringMvc provides a Converter interface
public class DateConvert implements Converter<String, Date> { @Override public Date convert(String stringDate) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { return simpleDateFormat.parse(stringDate); } catch (ParseException e) { e.printStackTrace(); } return null; }}Configure converter in spring.xml
<!-- Step 1: Create a custom date conversion rule --><bean id="dateConvert"/><!-- Step 2: Create conversion-Service and inject dateConvert--><bean id="conversionService"> <property name="converters"> <set> <ref bean="dateConvert"/> </set> </property></bean><!-- Step 3: Register the processor mapper/processor adapter and add the conversion-service property --><mvc:annotation-driven conversion-service="conversionService"/>
The above example explanation of SpringMVC passing date parameters to the background is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.