SpringMVC-使用converter转换器解决后台接收Date格式数据时The request sent by the client was syntactically incorrect ()
来源:互联网 发布:北京海量数据离职 编辑:程序博客网 时间:2024/05/17 22:42
前台有一个createtime的input框,比如输入 2012-12-12
<input id="createtime" name="createtime" />
controller,使用Date接收:
@RequestMapping("/addItems.action")public String addItems(Model model,String name, String detail, Double price, Date createtime){Items items = new Items();items.setName(name);items.setDetail(detail);items.setPrice(price);items.setCreatetime(createtime);itemsService.insert(items);return "";}
这种情况下,并不会进到这个方法,而是报400错误The request sent by the client was syntactically incorrect ()
这个问题,也即是springMvc如何进行参数类型的转换 , 以把client传过来一个String类型,转换为日期类型为例
回到顶部
步骤
1.controller
/** * 接收日期类型参数 * 注意: * springmvc 在接收日期类型参数时,如不做特殊处理 会出现400语法格式错误 * 解决办法 * 1.全局日期处理 * */ @RequestMapping("/test") public String test(Date birthday){ System.out.println(birthday); return "index"; }
2.自定义类型转换规则
SpringMvc提供了Converter接口,它支持从一个Object转换为另一个Object
/** * 全局日期处理类 * Convert<T,S> * 泛型T:代表客户端提交的参数 String * 泛型S:通过convert转换的类型
*/public class DateConvert implements Converter<String, Date> { @Override public Date convert(String stringDate) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { return simpleDateFormat.parse(stringDate); } catch (ParseException e) { e.printStackTrace(); } return null; }}
3.注册自定义的类型转换类
<!-- 第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性--> <mvc:annotation-driven conversion-service="conversionService"/> <!-- 第二步: 创建convertion-Service ,并注入dateConvert--> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <ref bean="dateConvert"/> </set> </property> </bean> <!-- 第一步: 创建自定义日期转换规则 --> <bean id="dateConvert" class="zpark.convert.DateConvert"/>
通过以上转换器,即可解决问题,在controller中就可以用Date数据类型来接收前天传递过来的数据了
阅读全文
0 0
- SpringMVC-使用converter转换器解决后台接收Date格式数据时The request sent by the client was syntactically incorrect ()
- The request sent by the client was syntactically incorrect解决
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect问题解决
- The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect ()问题
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect ()"解决办法
- The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect ()解决方法
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect ()
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect解决办法
- The request sent by the client was syntactically incorrect ()
- 根据给定起止时间,完成下载任务(大概类似于日历日程这种)
- spring_boot整合mybatis项目启动时报错
- javascript判断是电脑还是手机,android手机还是ios
- Location 对象
- 设计18
- SpringMVC-使用converter转换器解决后台接收Date格式数据时The request sent by the client was syntactically incorrect ()
- HttpClient发送Post请求(二)
- DIRECTORY_SEPARATOR
- 统计原理笔记 Notes for Statistics I
- 我的服务器开发之路-centos中crontab定时器的使用
- 不能用 ngOnChanges 侦测query结果集的变化, 要用 observable subscribe
- 初探ansible
- 使用font-size:0 来去掉inline-block元素之间的空隙
- 关于sitemesh装饰器的使用方式