This article mainly introduces the relevant knowledge of date format conversion in SpringMVC: used to solve the problem of abnormal date submission conversion. Has very good reference value. Let’s take a look at it with the editor
Solution to the problem of abnormal date submission conversion
Since date data has many formats, springmvc cannot convert StringConvert to date type. So you need to customize parameter binding. After the front-end Controller receives the request, it finds the annotated processor adapter, adapts the method marked by RequestMapping, and binds the formal parameters in the method. . In springmvc, you can customize the Converter on the processor adapter for parameter binding. This tag can be expanded if f7ba1f27e11c63617ca69c495697dd74 is used.
1. Customize the DataConvertor class and implement the Convertor interface
public class DateConverter implements Converter<String, Date> { @Override public Date convert(String source) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return simpleDateFormat.parse(source); } catch (ParseException e) { e.printStackTrace(); } return null; } }
2. Register the converter in springmvc.xmlconfiguration file
Method 1: Drive through annotations Method to load the converter
<!-- 配置mvc注解驱动 --> <mvc:annotation-driven conversion-service="conversionService"/> <!-- 配置日期转换器 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.rodge.ssm.converter.DateConverter"></bean> </set> </property> </bean>
Method 2: Configure through custom webBinder (not commonly used)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描带Controller注解的类 --> <context:component-scan base-package="cn.itcast.springmvc.controller" /> <!-- 转换器配置 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.itcast.springmvc.convert.DateConverter"/> </set> </property> </bean> <!-- 自定义webBinder --> <bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService" ref="conversionService" /> </bean> <!--注解适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="webBindingInitializer" ref="customBinder"></property> </bean> <!-- 注解处理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- 加载注解驱动 --> <!-- <mvc:annotation-driven/> --> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- jsp前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- jsp后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
Note: This method requires independent configurationProcessor mapper, adapter, no longer use254e094b9655a938c1e3112130c37d95
The above is the detailed content of Detailed explanation of sample code for date format conversion in SpringMVC. For more information, please follow other related articles on the PHP Chinese website!