Spring MVC RequestMappingHandlerAdapter
来源:原创 发布时间:2015-04-06 归档:springmvc
开发环境 :
JDK 7
Maven 3
Tomcat 7
Spring 4.1.5
Eclipse Luna
RequestMappingHandlerAdapter 负责处理请求参数到处理方法参数的绑定以及数据类型转换等工作。
数据类型转换配置示例
<!-- 配置数据转换服务 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="org.lychie.converter.DateConverter" p:pattern="yyyy-MM-dd" /> </set> </property> </bean> <!-- 配置数据绑定, 依赖数据转换服务进行绑定 --> <bean id="webBindingInitializer" 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="webBindingInitializer" /> </bean>org.lychie.converter.DateConverter
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.util.StringUtils;
import org.lychie.exception.ConverterCastException;
import org.springframework.core.convert.converter.Converter;
public class DateConverter implements Converter<String, Date> {
private String pattern;
@Override
public Date convert(String source) {
if (StringUtils.hasText(source)) {
DateFormat dateFormat = new SimpleDateFormat(pattern);
dateFormat.setLenient(false);
try {
return dateFormat.parse(source);
} catch (ParseException e) {
throw new ConverterCastException(e);
}
}
throw new ConverterCastException("For input : " + source);
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
}
Controller
@RequestMapping(value = "/register", method = GET)
public String registerDate(ModelMap model, Date date) {
if (date == null) {
model.addAttribute("date", "NULL");
} else {
DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
model.addAttribute("date", dateFormat.format(date));
}
return "result";
}
spring mvc 支持标准的 UTC 时间格式, 若时间不是标准的 UTC 格式, 需要对其进行转换, 否则会引发 400 异常。
GET /register?date=2015-04-06 --> 2015年04月06日
防止 JSON 数据中文乱码配置示例
GET /register?date=2015-04-06 --> 2015年04月06日
<!-- 配置HTTP消息转换器 --> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=utf-8</value> <value>application/json;charset=utf-8</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> </list> </property> </bean>
或 :
<!-- 配置HTTP消息转换器 --> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="utf-8" /> </bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> </list> </property> </bean>
StringHttpMessageConverter 默认采用 ISO-8859-1 字符集, 中文显示成乱码。解决办法是采用 UTF-8、GBK、GB2312 等兼容中文的字符集。
示例代码下载