package com.synnex.web.common;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
public class DateBindingInitializer implements WebBindingInitializer {
private String format;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public void initBinder(WebDataBinder binder, WebRequest request) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, false));
binder.registerCustomEditor(String.class,new StringTrimmerEditor(false));
binder.registerCustomEditor(EventStageEnum.class, new EnumPropertyEditor<EventStageEnum>(EventStageEnum.class));
}
}
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.synnex.web.common.DateBindingInitializer">
<property name="format" value="MM/dd/yyyy"></property>
</bean>
</property>
</bean>
如果是局部binder,则可以在Controller里这样写:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
如果是String类型转Enum,则可以这样做:(String的值必须要与Enum对应,区分大小写的)
package com.synnex.web.bind;
import java.beans.PropertyEditorSupport;
public class EnumPropertyEditor<E extends Enum<E>> extends PropertyEditorSupport {
private Class<E> clazz;
public EnumPropertyEditor(Class<E> clazz) {
this.clazz = clazz;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
try {
setValue(Enum.valueOf(clazz, text));
} catch (Exception ex) {
setValue(null);
}
}
}
网上还发现另一种写法:http://quicker.iteye.com/blog/669061
spring mvc 配置最小化非标签
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<context:component-scan base-package="com.hike.wechat.controller" />
<bean class="com.hike.common.exception.BizHandlerExceptionResolver">
<property name="defaultErrorView" value="error/jsonError" />
</bean>
<!--
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="ignoreAcceptHeader" value="true" />
<property name="favorPathExtension" value="true"/>
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="com.alibaba.fastjson.support.spring.FastJsonJsonView"/>
<bean class="com.hike.common.extend.MarshallingViewEx">
<constructor-arg ref="oxmMarshaller"></constructor-arg>
</bean>
</list>
</property>
</bean>
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="jsonHttpMessageConverter" />
<!-- <ref bean="xmlHttpMessageConverter" /> -->
</util:list>
</property>
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator">
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"></bean>
</property>
<property name="conversionService">
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
</property>
</bean>
</property>
</bean>
<bean id="jsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" />
<bean id="xmlHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="oxmMarshaller" />
<property name="unmarshaller" ref="oxmMarshaller" />
</bean>
<bean id="oxmMarshaller" class="com.hike.common.extend.XStreamMarshallerEx"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxInMemorySize">
<value>10485760</value><!--1024*1024*10b=10485760b=10M -->
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean> -->
</beans>