Spring MVC 之类型转换(五)
虽然SpringMVC可以自动绑定多种数据类型,但是有时候有些特殊的数据类型还是会在绑定时发生错误,需要我们自己书写类型转换完成绑定。
1、属性编辑器(传统方式)
控制器:
@RequestMapping(value="/login.do")
public String login(UserBean user){
log.info(user.getUsername());
log.info(user.getBirthday());
return "index";
}
// 自定义一个属性编辑器,用于转换时间类型
@InitBinder
public void converterStringDate(WebDataBinder binder){
binder.registerCustomEditor(Date.class, new DateEditor());
}
可以通过重写PropertyEditorSupport中的setAsText()定义自己的转换规则
package com.cy.springannotation.controller; import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class DateEditor extends PropertyEditorSupport { @Override
public void setAsText(String text) throws IllegalArgumentException {
Date date = null;
try {
if(text.contains("-")){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
date = sf.parse(text);
}else {
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
date = sf.parse(text);
}
} catch (ParseException e) {
e.printStackTrace();
}
this.setValue(date);
} }
2、类型转换器
@RequestMapping(value="/login.do")
public String login(UserBean user){
log.info(user.getUsername());
log.info(user.getBirthday());
return "index";
}
全局类型转换器
package com.cy.springannotation.controller; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.core.convert.converter.Converter;
/**
* 全局类型转换器
* @author acer
*
*/
public class DateConvert implements Converter<String, Date>{
@Override
public Date convert(String text) {
Date date = null;
try {
if(text.contains("-")){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
date = sf.parse(text);
}else {
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
date = sf.parse(text);
} } catch (ParseException e) {
e.printStackTrace();
}
return date;
} }
类型转换器需要在配置文件中配置:
<!--开启注解 -->
<mvc:annotation-driven conversion-service="tc" /> <!--类型转换器工厂 --> <bean id="tc" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.cy.springannotation.controller.DateConvert" />
</list>
</property>
</bean>
共同的jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登录页面</title>
<script type="text/javascript" src="<%=basePath%>js/jquery-2.1.4.js"></script>
<script type="text/javascript"> </script>
</head>
<body>
<form action="login.do" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>出生日期</td>
<td><input type="text" name="birthday"/></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="提交"/> </td>
</tr>
</table>
</form>
</body>
</html>
结果显示:


Spring MVC 之类型转换(五)的更多相关文章
- 2017.3.31 spring mvc教程(五)Action的单元测试
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- Spring mvc参数类型转换
1,需求 有时候我们接收到的参数为String类型的,但是我们需要将它们转化为其他类型的如:date类型,枚举类型等等,spring mvc为我们提供了这样的功能. 2,配置文件 在springmvc ...
- spring mvc 参数类型转换
实现方式以字符串转Date为例说明: 全局配置 第一种:实现 Converter 接口 实现类: public class StringToDateConveter implements Conver ...
- Spring MVC学习(五)---ModelAndView没有明显申明name
看图不解释: 对于这种写法: new ModelAndView().addObject(XXX)
- spring笔记2 spring MVC的基础知识2
2,spring MVC的注解驱动控制器,rest风格的支持 作为spring mvc的明星级别的功能,无疑是使得自己的code比较优雅的秘密武器: @RequestMapping处理用户的请求,下面 ...
- spring笔记1 spring MVC的基础知识1
1,spring MVC的流程 优秀的展现层框架-Spring MVC,它最出彩的地方是注解驱动和支持REST风格的url. 流程编号 完成的主要任务 补充 1 用户访问web页面,发送一个htt ...
- Spring MVC 教程,快速入门,深入分析
http://elf8848.iteye.com/blog/875830/ Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门 资源下载: ...
- Spring MVC 教程
目录 一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明 ...
- (转)Spring MVC
资源下载: Spring_MVC_教程_快速入门_深入分析V1.1.pdf SpringMVC核心配置文件示例.rar 作者:赵磊 博客:http://elf8848.iteye.com 目录 一.前 ...
随机推荐
- Linux vim的安装和配置:
1:首先就碰到一个问题 程序 'vim' 已包含在下列软件包中: * vim * vim-gnome * vim-tiny * vim-athena * vim-gtk * vim-nox 请尝试:s ...
- Unity脚本在层级面板中的执行顺序测试3
断断续续的写了3篇,以后有时间可以做成一个系列了 前面2篇测试了GameObject的顺序,以及Awake和OnEnable的时机: Unity脚本在层级面板中的执行顺序测试1 http://www. ...
- git status message - Your branch is ahead of origin/master by X commits
git reset --hard origin/master git status FAQ: When I issue the "git status" command, I se ...
- 新蒂下午茶体基本版SentyTEA-Basic
一.目前的最新版新蒂下午茶体包含了7600+常用汉字,每个字都是手写而成,是一套充满手写感的中文字体,轻松.惬意,如同慢饮一杯下午茶.SentyTEA-Basic.ttf 这个一个新蒂下午茶体基本版 ...
- 如何使用不同参数组合生成独立的TestCase函数(Python)
在使用selenium2 Python做自动化测试的时候遇到个问题,写一个testcase 生成报告后,会有一个case的执行状态记录.这样我们写一个登录功能的自动化用例,只写一个case显然是不行的 ...
- ContentProvider官方教程(8)自定义MIME
MIME Type Reference Content providers can return standard MIME media types, or custom MIME type stri ...
- ios沙盒路径
http://www.cnblogs.com/ios-wmm/p/3299695.html iOS沙盒路径的查看和使用 NSString *path = NSHomeDirectory();//主目录 ...
- SqlSever基础 delete 删除一个表中的所有数据
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- SqlSever基础 datepart函数 返回这一秒已经过去了多少毫秒
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- Web自定义协议,BS端启动CS端,
实例 1.准备CS项目,windows窗体应用程序,拖进来一个label控件来接受BS的参数,并显示,右击生成,复制该文件的bin目录下的exe,例如放在以下路径,例如C:\\simu\\下, 2.编 ...