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 目录 一.前 ...
随机推荐
- php编译器
WordPress http://pan.baidu.com/s/1eQnOnv0 epp3: http://pan.baidu.com/s/1pJKFOD1 配合xampp: http://pan. ...
- UVa 10054,欧拉回路
题目链接:https://uva.onlinejudge.org/external/100/10054.pdf 题目链接:http://vjudge.net/contest/132239#proble ...
- PLsql设置
1.类SQL PLUS窗口:File->New->Command Window,这个类似于oracle的客户端工具sql plus,但比它好用多了. 2.设置关键字自动大写:Tools-& ...
- python学习笔记三 函数(基础篇)
函数 内置函数 常用的内建函数: type() 列出指定对象的类型 help() 能够提供详细的帮助信息 dir() 将对象的所有特性列出 vars() 列出当前模块的所有变量 file, ...
- wireshark抓包直观图解 TCP三次握手/四次挥手详解
转http://www.seanyxie.com/category/linux/ 作者:seanyxie | 一. TCP/IP协议族 TCP/IP是一个协议族,通常分不同层次进行开发,每个层次负 ...
- 十二 个经典 Linux 进程管理命令介绍
执行中的程序在称作进程.当程序以可执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源.内存.安全属性和与之相关的状态.可以有多个进程关联到同一个程序,并同时执行不会互相干扰.操作系统 ...
- 从exchange2010上面删除特定主题或特定时间的邮件
昨天在上班的公交上接到同事电话,说他的的部门老大发错了一封邮件到另外一个同事邮箱了,问我能不 能去那个同事的邮箱里面删除,我一想,之前在网上看到过资料,到了公司趁那个误接收邮件的同事还没有来,在服务器 ...
- Android开发开始--环境搭建
一.搭建Android开发环境 1.JDK (Java Development Kit) 2.Eclipse 3.Android SDK (Software Development Kit) 4.AD ...
- C#获取CPUID(MD5输出),网卡ID,主DNS,备用DNS
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 【leetcode❤python】299. Bulls and Cows
class Solution(object): def getHint(self, secret, guess): """ :type ...