`

SpringMVC @InitBinder webBindingInitializer

 
阅读更多

1:

注册Controller内的Binder:

 

@InitBinder
	 public void initBinder(WebDataBinder binder) {
	 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	 dateFormat.setLenient(false);
	 binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// true:允许输入空值,false:不能为空值
	 }

 2:或注册全局的binder:

 

<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
				<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
				<bean class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/plain;charset=UTF-8</value>
						</list>
					</property>
				</bean>
				<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
					<constructor-arg index="0">
						<util:constant static-field="com.systoon.smvc.utils.json.JsonUtils.OBJECT_MAPPER"/>
					</constructor-arg>
					<property name="supportedMediaTypes">
						<list>
							<value>text/plain;charset=UTF-8</value>
							<value>application/json;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
		<!-- 注册自定义转换器 -->
		<property name="webBindingInitializer">
			<bean class="com.systoon.smvc.converter.SpringMVCConverter">
				<property name="datePattern" value="yyyy-MM-dd HH:mm:ss,yyyy-MM-dd"/>
			</bean>
		</property>
	</bean>

<!-- 自动扫描且只扫描@Controller -->
	<context:component-scan base-package="com.systoon.advice.web.*.controller" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

<!-- 注意放到最后,否则全局binder不起作用 -->
<mvc:annotation-driven  />

 

以上的:<mvc:annotation-driven /> 必须放到最后,否则全局binder不起作用!!!!!

分享到:
评论

相关推荐

    SpringMVC自定义属性编辑器详解及实例

    自定义springMVC的属性编辑器主要有两种方式,一种是使用@InitBinder标签在运行期注册一个属性编辑器,这种编辑器只在当前Controller里面有效;还有一种是实现自己的 WebBindingInitializer,然后定义一个...

    springmvc类型转换.md

    import org.springframework.web.bind.support.WebBindingInitializer; import org.springframework.web.servlet.mvc.method.annotation.InitBinderMethod; public class MyController { @InitBinder public ...

    解决springmvc关于前台日期作为实体类对象参数类型转换错误的问题

    &lt;property name="webBindingInitializer"&gt; ``` 解决方案3:使用@InitBinder注解 使用@InitBinder注解,注册一个父类注解,注册一个父类Controller(BaseController),其他Controller继承它。例如:...

    深入理解Spring MVC的数据转换

    3. 通过WebBindingInitializer接口装配的全局自定义编辑器 六、Formater Formater是Spring MVC中的一个重要组件,它提供了一个格式化转换机制。Formater的使用示例如下所示: public class DateFormatter extends...

    SpringMVC自定义参数绑定实现详解

    SpringMVC自定义参数绑定实现详解 SpringMVC自定义参数绑定实现详解主要介绍了SpringMVC自定义参数绑定实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以...

Global site tag (gtag.js) - Google Analytics