SpringMVC绑定参数之类型转换有三种方式:
1. 实体类中加日期格式化注解
@DateTimeFormat(pattern="yyyy-MM-dd hh:MM")
private Date creationTime;
2.在Controller类的方法中加入一段代码
/**
* 在controller层中加入一段数据绑定代码
* @param webDataBinder
*/
@InitBinder
public void initBinder(WebDataBinder webDataBinder) throws Exception{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
simpleDateFormat.setLenient(false);
webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
}
备注:自定义类型转换器必须实现PropertyEditor接口或者继承PropertyEditorSupport类
写一个类 extends propertyEditorSupport(implements PropertyEditor){
public void setAsText(String text){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy -MM-dd hh:mm");
Date date = simpleDateFormat.parse(text);
this.setValue(date);
}
public String getAsTest(){
Date date = (Date)this.getValue();
return this.dateFormat.format(date);
}
}
3.(spring 3.0以前使用正常,以后的版本需要使用<mvc:annotation-driven/>注册使用)使用xml配置实现类型转换(系统全局转换器)
1.注册conversionservice
<!-- 注册ConversionService-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.ezubo.global.portal.util.StringToDateConverter">
<constructor-arg index="0" value="yyyy-MM-dd hh:mm"/>
</bean>
</set>
</property>
</bean>
备注:StringtoDateConverter.java的实现:
/**
* Created by 苏 on 15-10-13.
*/
public class StringToDateConverter implements Converter<String,Date> {
private static final Logger logger = LoggerFactory.getLogger(StringToDateConverter.class);
private String pattern;
public StringToDateConverter(String pattern){
this.pattern = pattern;
}
public Date convert(String s) {
if(StringUtils.isBlank(s)){
return null;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
simpleDateFormat.setLenient(false);
try{
return simpleDateFormat.parse(s);
}catch(ParseException e){
logger.error("转换日期异常:"+e.getMessage() , e);
throw new IllegalArgumentException("转换日期异常:"+e.getMessage() , e);
}
}
}
2.使用 ConfigurableWebBindingInitializer 注册conversionService
<!--使用 ConfigurableWebBindingInitializer 注册conversionService-->
<bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService"/>
</bean>
3.注册ConfigurableWebBindingInitializer到RequestMappingHandlerAdapter
<!-- 注册ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="webBindingInitializer"/>
<!-- 线程安全的访问session-->
<property name="synchronizeOnSession" value="true"/>
</bean>
此时可能有人会问,如果我同时使用 PropertyEditor 和 ConversionService,执行顺序是什么呢?内部首先查找PropertyEditor 进行类型转换,如果没有找到相应的 PropertyEditor 再通过 ConversionService进行转换。
4.(spring 3.2以后使用正常)使用<mvc:annotation-driven/>注册conversionService
具体原因请参考:
1.注册ConversionService
<!-- 注册ConversionService-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.ezubo.global.portal.util.StringToDateConverter">
<constructor-arg index="0" value="yyyy-MM-dd hh:mm"/>
</bean>
</set>
</property>
</bean>
2.需要修改springmvc-servlet xml配置文件中的annotation-driven,增加属性conversion-service指向新增的 conversionService。
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
<!--转换时设置特性-->
<property name="features">
<array>
<!--避免默认的循环引用替换-->
<ref bean="DisableCircularReferenceDetect"/>
<ref bean="WriteMapNullValue"/>
<ref bean="WriteNullStringAsEmpty"/>
<ref bean="WriteNullNumberAsZero"/>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
分享到:
相关推荐
SpringMVC 数据类型转换是Spring Web MVC框架中的一个重要部分,主要负责将HTTP请求中的字符串数据转换为控制器方法中期望的数据类型。在2021-2022年的专题资料中,这一主题聚焦于Spring 3之后引入的新特性和改进,...
SpringMVC实现自定义类型转换器 SpringMVC框架提供了强大的类型转换功能,以便将HTTP请求参数转换成Java对象。但是,默认情况下,SpringMVC只能转换基本数据类型,如String转换成Integer、Double等。如果需要将...
springmvc注解式控制器的数据验证、类型转换及格式化 SpringMVC数据验证 参数传递
解决SpringMVC关于前台日期作为实体类对象参数类型转换错误的问题有多种方法,下面我们将介绍三种解决方案。 解决方案1:使用@DateTimeFormat注解 在对应的实体类属性上加入@DateTimeFormat(pattern = "yyyy-MM-dd...
SpringMVC 提供了多种参数绑定的方式,包括默认的参数绑定、简单类型参数绑定、POJO 类型参数绑定、包装类型参数绑定等,除此之外还提供了自定义参数转换的功能,允许开发者根据需求实现自定义的参数转换。
问题2:前端参数未转换为JSON格式,会导致403错误。需使用`JSON.stringify()`将JavaScript对象转化为JSON字符串。 3. 传数组参数 在POST请求中,可以使用@RequestParam接收数组。当数组作为请求参数时,需要在参数...
SpringMVC提供了多种方式来获取请求参数,包括通过ServletAPI、控制器方法的形参、@RequestParam、@RequestHeader、@CookieValue等方式。 五、域对象共享数据 SpringMVC提供了多种方式来共享数据,包括使用...
在Spring MVC中,类型转换器(Type Converter)是框架的核心组件之一,负责将HTTP请求中的字符串数据转换为控制器方法所需的参数类型。有时,Spring MVC内置的类型转换器无法满足所有需求,比如处理特殊格式的日期...
本指南提供了 SpringMVC 的核心思想、框架特点、入门示例、参数传递、标签介绍、拦截器、类型转换、JSON 格式数据的输入和输出、文件上传、国际化和本地化、验证等方面的知识点,旨在帮助开发者快速掌握 SpringMVC ...
Spring MVC提供了内置的类型转换器,但当系统需要处理自定义数据类型时,可能需要自定义类型转换器。本篇文章将详细讲解如何在Spring MVC中实现自定义类型转换器。 首先,自定义类型转换器需要实现Spring的核心接口...
这是第三种方法的改进,同样是将表单数据转换为JSON字符串,但可以处理更复杂的表单,包括多选控件。前端通过JavaScript将整个表单(可能包含多选框的值)转换为JSON,后端同样使用Jackson进行反序列化。然而,这种...
SpringMVC 框架知识点总结 SpringMVC 是一个基于 Java 语言的 Web 应用程序框架,主要用于构建 Web 应用...* 提供了一种机制来将请求的参数转换为 Java 对象 * 可以通过注入 ConversionService 来实现自定义参数绑定
"Spring MVC处理参数中的枚举类型通用实现方法" 在开发过程中,枚举类型数据的处理是一个非常重要的方面。 Spring MVC作为一个流行的Java ...标签:springmvc工作流程 springmvc 参数的枚举类型转换 springmvc参数绑定
在Spring MVC框架中,类型转换和验证是两个关键的组件,它们确保了从HTTP请求接收的数据能够正确地映射到控制器方法的参数上,并且在必要时进行验证以确保数据的有效性。以下是对这两个概念的深入解析: 1. **类型...
在SpringMVC中,有三种主要的界面跳转方式: 1. **视图解析器(View Resolver)**:这是最常用的方式。当你在控制器方法中返回一个字符串,比如"success",SpringMVC会查找配置的视图解析器,如`...
请求体通常包含JSON、XML或其他格式的数据,而查询参数和表单参数则以不同的方式存在。Content-Type头部用来指示请求体或响应体的数据格式,例如:text/plain、application/json或application/xml。Accept头部则用于...
第二步是使用 SpringMVC 的类型转换机制,例如使用 @InitBinder 注解或使用 WebDataBinder 对象来实现类型转换。 在上面的例子中,我们可以新建一个名为 DateConverter 的类,实现 Converter 接口,以便于将 String...
它可以处理GET和POST请求中的简单类型参数,通过`request.getParameter()`获取。它支持两个属性:`value`和`required`。`value`用于指定参数名,`required`表示参数是否必须存在: ```java @RequestMapping("/...
本文将详细介绍 Spring MVC 中用于接收参数的几种常见注解,包括它们的用途、用法以及适用场景。 1. **@PathVariable** `@PathVariable` 用于从请求的 URI 模板中提取变量值。当定义了一个带有占位符 `{paramId}` ...
SpringMVC提供了类型转换的机制,例如Converter接口,可以自定义类型转换器来实现类型转换。 执行流程 SpringMVC的执行流程主要包括以下几个步骤: 1. 在地址栏输入对应的URI。 2. 由核心控制器拦截对应的请求,...