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 目录 一.前 ...
随机推荐
- asp.net学习资料,mvc学习资料
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/adding-validation-to-the-model
- 2016年省赛G题, Parenthesis
Problem G: Parenthesis Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 398 Solved: 75[Submit][Status ...
- HTML+CSS编写规范
在任何一个项目或者系统开发之前都需要定制一个开发约定和规则,这样有利于项目的整体风格统一.代码维护和扩展.由于Web项目开发的分散性.独立性.整合的交互性等,所以定制一套完整的约定和规则显得尤为重要. ...
- [Django_1_0]初次见面
Django 初次见面 文章将写安装和第一次使用时候的操作.文章是照着文档做的,但是以后的内容会有不一样. 安装 pip install django 我这里是使用python3的,也可以使用 pip ...
- 如何在linux下查看gpu信息
~$ lspci | grep -i vga01:00.0 VGA compatible controller: NVIDIA Corporation GF119 [GeForce GT 610] ( ...
- 局域网无法访问vmware虚拟机WEB服务器解决办法
环境:虚拟机服务器是centos,apache+php+mysql环境,但是局域网无法访问 1.本机能ping通虚拟机 2.虚拟机也能ping通本机 3.虚拟机能访问自己的web 4.本机无法访问虚拟 ...
- 2016-2017 CT S03E05: Codeforces Trainings Season 3 Episode 5 (2016 Stanford Local Programming Contest, Extended) E
链接:http://codeforces.com/gym/101116 学弟写的,以后再补 #include <iostream> #include <algorithm> # ...
- 2016 Al-Baath University Training Camp Contest-1 G
Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...
- 2016年12月12日 星期一 --出埃及记 Exodus 21:7
2016年12月12日 星期一 --出埃及记 Exodus 21:7 "If a man sells his daughter as a servant, she is not to go ...
- 批量清除.svn 或 _svn
Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN]@=&q ...