springmvc的类型转换
一、springmvc的类型转换
(一)默认情况下,springmvc内置的类型转换器只能
将“yyyy/MM/dd”类型的字符串转换为Date类型的日期
情境一:
而现在我们无法得知用
户会输入什么日期格式的数据,所以,内置的类型转换器无法转换其他日期格式的类型
为了方便程序,减少代码量,我们抽离出自己的类型转换器
此种方法也有弊端,就是讲几个类唯一的继承权用在了类型转换器上,但也是没有办法的办法
步骤一:
定义自己的类型转换器 继承一个父接口 Converter<S, T>
s:代表源数据类型
T:代表目标数据类型
package cn.yxj.convertion; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern; import org.springframework.core.convert.converter.Converter; public class MyConvertion implements Converter<String, Date>{ public Date convert(String source) { /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");*/
SimpleDateFormat sdf=getDateFormat(source);
try {
return sdf.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
} private SimpleDateFormat getDateFormat(String source) {
//进行正则匹配
//2016-12-14
//2016/12/14
//
if(Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)){
return new SimpleDateFormat("yyyy-MM-dd");
}else if(Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)){
return new SimpleDateFormat("yyyy/MM/dd");
}else if(Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)){
return new SimpleDateFormat("yyyyMMdd");
}else if(Pattern.matches("^\\d{4}年\\d{2}月\\d{2}日$", source)){
return new SimpleDateFormat("yyyy年MM月dd日");
}
return null;
} }
步骤二、在配置文件中注册自定义类型转化器,将该类交由spring容器管理
<!-- 01.注册自定义的类型转换器 -->
<bean id="myConvertion" class="cn.yxj.convertion.MyConvertion"></bean> <!--02.注册类型转换器的服务工厂 产生转换对象 -->
<bean id="convertionservice" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters" ref="myConvertion"></property>
</bean>
<!--03. -->
<mvc:annotation-driven conversion-service="convertionservice"/>
配置完成
(二) 情景二:
当我们在前台输入如下信息 ,年龄为string不能装配成后台的int类型转向400错误页面,
面对这种情况我们更想看到的是回到初始页面

解决方案:这种情况就是出现了类型转换异常
我们就采用异常处理机制,其实再出现类型转换异常时,请求就不会再进入处理器方法,而是被我们自定的的异常处理方法所捕获
在处理器类中加入异常处理方法,完成重定向的功能
//当出现类型转换异常时,跳回index.jsp
@ExceptionHandler(TypeMismatchException.class)
public ModelAndView getBack(HttpServletRequest request,Exception ex){
ModelAndView mv=new ModelAndView();
mv.setViewName("/index.jsp");
return mv;
}
(三)情景三:
完成上述操作后,我们现在再来思考一个问题,如果表单上有很多条数据,用户提交失败,重定向后之前所填的数据完全丢失,那用户的内心估计是崩溃的
这是我们就应该来考虑“数据回显”的问题了,最好,我们可以提示出那条数据出错了
下面我们就重新优化异常处理机制
首先我们查看一下异常的消息,我们会发现一个规律,利于我们进行特定数据异常的定位 所以下面我们使用ex.getMessage().contains(birthday)

//当出现类型转换异常时,跳回index.jsp
@ExceptionHandler(TypeMismatchException.class)
public ModelAndView getBack(HttpServletRequest request,Exception ex){
ModelAndView mv=new ModelAndView();
String birthday=request.getParameter("birthday");
String age=request.getParameter("age");
mv.addObject("birthday",birthday);
mv.addObject("age",age);
if(ex.getMessage().contains(birthday)){
mv.addObject("birthdayerror","日期格式不正确");
}
if(ex.getMessage().contains(age)){
mv.addObject("ageerror","年龄格式不正确");
}
mv.setViewName("/index.jsp");
return mv;
}
结束
springmvc的类型转换的更多相关文章
- spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二)
spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二) >>>>>>>>>>>>>>&g ...
- spring参数类型异常输出,SpringMvc参数类型转换错误输出
spring参数类型异常输出, SpringMvc参数类型转换错误输出 >>>>>>>>>>>>>>>> ...
- 转:SpringMVC之类型转换Converter(GenericConverter)
转: http://blog.csdn.net/fsp88927/article/details/37692215 SpringMVC 之类型转换 Converter 1.1 目录 1.1 目录 1. ...
- SpringMVC 之类型转换Converter详解转载
SpringMVC之类型转换Converter详解 本文转载 http://www.tuicool.com/articles/uUjaum 1.1 目录 1.1 目录 1.2 ...
- SpringMVC 之类型转换Converter 源代码分析
SpringMVC 之类型转换Converter 源代码分析 最近研究SpringMVC的类型转换器,在以往我们需要 SpringMVC 为我们自动进行类型转换的时候都是用的PropertyEdito ...
- SpringMVC之类型转换Converter
(转载:http://blog.csdn.net/renhui999/article/details/9837897) 1.1 目录 1.1 目录 1.2 前言 1.3 ...
- [转]SpringMVC日期类型转换问题三大处理方法归纳
http://blog.csdn.net/chenleixing/article/details/45190371 前言 我们在SpringMVC开发中,可能遇到比较多的问题就是前台与后台实体类之间日 ...
- springmvc参数类型转换三种方式
SpringMVC绑定参数之类型转换有三种方式: 1. 实体类中加日期格式化注解 @DateTimeFormat(pattern="yyyy-MM-dd hh:MM&quo ...
- SpringMVC日期类型转换问题三大处理方法归纳
方法一:实体类中加日期格式化注解 @DateTimeFormat(pattern = "yyyy-MM-dd") private Date receiveAppTime; 方法二: ...
随机推荐
- SSIS 处理NULL
不同于SQL Server中NULL表示值是未知的(Unknown Value),没有数据类型,但是,在SSIS中,NULL是有数据类型的,要获取某一个NULL值,必须指定数据类型,例如,变量 Int ...
- Lookup component 用法
Lookup component 类似于Tsql的join子句, select a.* ,b.* from dbo.tis a left join dbo. tdes b on a.code=b.co ...
- jQuery 2.0.3 源码分析 Deferred概念
JavaScript编程几乎总是伴随着异步操作,传统的异步操作会在操作完成之后,使用回调函数传回结果,而回调函数中则包含了后续的工作.这也是造成异步编程困难的主要原因:我们一直习惯于“线性”地编写代码 ...
- codeforces B. Ohana Cleans Up
B. Ohana Cleans Up Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid ...
- 创建外网 ext_net - 每天5分钟玩转 OpenStack(104)
虽然外部网络是已经存在的网络,但我们还是需要在 Neutron 中定义外部网络的对象,这样 router 才知道如何将租户网络和外部网络连接起来. 上一节我们已经为创建外部网络配置了ML2,本节将通过 ...
- geotrellis使用(七)记录一次惨痛的bug调试经历以及求DEM坡度实践
眼看就要端午节了,屌丝还在写代码,话说过节也不给轻松,折腾了一天终于解决了一个BUG,并完成了老板安排的求DEM坡度的任务,那么就分两段来表. 一.BUG调试 首先记录一天的BUG调试,简单copy了 ...
- 使用yield进行异步流程控制
现状 目前我们对异步回调的解决方案有这么几种:回调,deferred/promise和事件触发.回调的方式自不必说,需要硬编码调用,而且有可能会出现复杂的嵌套关系,造成"回调黑洞" ...
- JQuery中使用Ajax实现诸如登录名检测等异步请求Demo
上一篇博客介绍了注册登录时一次性图形验证码的工具类的编写,这篇随笔同样是我在写用jquery中ajax实现登录信息检测的异步请求功能的笔记,在各个网站进行信息用户注册时,需要在不刷新页面的情况下对注册 ...
- Azure ARM (14) 设置ARM VM的Availability Set
<Windows Azure Platform 系列文章目录> 参考资料:https://gallery.technet.microsoft.com/Set-Azure-Resource- ...
- Mybatis XML 映射配置文件 -- 熟悉配置
来源:http://www.mybatis.org/mybatis-3/zh/configuration.html properties mybatis读取属性顺序. 如果属性在不只一个地方进行了配置 ...