1.配置web.xml文件

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

  2.自定义日期变流器

import org.springframework.beans.TypeMismatchException;
import org.springframework.core.convert.converter.Converter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
//converter<String,Date> Converter<S,T> S:Source  T:Target;
public class MyDateConverter implements Converter<String, Date> {
public DateFormat getDateFormat(String str) {
DateFormat sdf = null;
//str 2017/08/20 str 2017-08-20 str 2017年08月20日
//模式匹配 正则 元字符 (用来匹配用的 或者 限定用的)
if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", str)) {
sdf = new SimpleDateFormat("yyyy-MM-dd");
} else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", str)) {
sdf = new SimpleDateFormat("yyyy/MM/dd");
} else if (Pattern.matches("^\\d{4}年\\d{2}月\\d{2}日$", str)) {
//这里是中文字符,所以要在web.xml里配置filter来一定编码格式UTF-8
sdf = new SimpleDateFormat("yyyy年MM月dd日");
} else {
throw new TypeMismatchException("", Date.class);    //异常抛出
}
return sdf;
}
public Date convert(String str) {
//特定格式的字符串,转成日期
DateFormat sdf = getDateFormat(str);
try {
Date date = sdf.parse(str);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}

  3.日期格式处理器

import org.springframework.beans.TypeMismatchException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
@Controller
public class FirstController {
@ExceptionHandler(TypeMismatchException.class)
public ModelAndView resovleException(Exception ex) {
System.out.println("111111111111111111");
ModelAndView mv = new ModelAndView();
if (ex instanceof TypeMismatchException) {
mv.setViewName("/typeconverter.jsp");
}
//携带异常日志到页面
mv.addObject("datamsg", ex.getMessage());
return mv;
}
@RequestMapping("/first")
//类型转化工作一定是在真正的handler方法执行前执行的
public String doFirst(Date birthday, int age) throws Exception {
System.out.println("222222222222222222222");
System.out.println(birthday + "===========");
System.out.println(age + "===============");
return "/index.jsp";
}
}

  4.映射器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="cn.happy"/> <!--注册转换器-->
<bean id="dateConverter" class="cn.happy.converter.MyDateConverter"/> <!--注册服务工厂-->
<bean id="serviceFactory" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters" ref="dateConverter"/>
</bean> <!--注册驱动去关联 注册服务工厂-->
<mvc:annotation-driven conversion-service="serviceFactory"/>
</beans>

  5.1:注册页面

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>类型转换</h1>
<form action="${pageContext.request.contextPath}/first" method="post">
出生日期:<input name="birthday" value="${mydate}"/><span>${datamsg}</span><br/><br/>
年龄:<input name="age" value="${age}"/><br/><br/>
<input type="submit" value="注册"/>
</form>
</body>
</html>

  5.2:成功页面

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

  测试结果1:

  测试结果2:

  测试结果3:

  测试结果4:

SpringMVC09 Converter变流器、数据回显、异常测试的更多相关文章

  1. SpringMVC由浅入深day02_5数据回显_6异常处理器

    5 数据回显 5.1 什么数据回显 表单提交失败需要再回到表单页面重新填写,原来提交的数据需要重新在页面上显示. 5.2 pojo数据回显方法 1.springmvc默认对pojo数据进行回显. po ...

  2. 表单很多数据项录入的时候,提交controller发生异常,数据回显。

    1.添加的情况(Model传递Form Data) request.getSession().setAttribute("car", car); //抛出异常的时候,数据回显. 2 ...

  3. SpringMVC【参数绑定、数据回显、文件上传】

    前言 本文主要讲解的知识点如下: 参数绑定 数据回显 文件上传 参数绑定 我们在Controller使用方法参数接收值,就是把web端的值给接收到Controller中处理,这个过程就叫做参数绑定.. ...

  4. SpringMVC学习(四)———— 数据回显与自定义异常处理器

    一.数据回显技术 Springmvc默认支持对pojo类型的数据回显,默认不支持简单类型的数据回显 1.1.什么是数据回显? 在信息校验时,如果发生校验错误,那么把校验的数据信息,依然停留在当前页面, ...

  5. springmvc(五) 数据回显与自定义异常处理器

    这章讲解一下springmvc的数据回显和自定义异常处理器的使用,两个都很简单 --WH 一.数据回显技术 Springmvc默认支持对pojo类型的数据回显,默认不支持简单类型的数据回显 1.1.什 ...

  6. SpringMVC第五篇【方法返回值、数据回显、idea下配置虚拟目录、文件上传】

    Controller方法返回值 Controller方法的返回值其实就几种类型,我们来总结一下-. void String ModelAndView redirect重定向 forward转发 数据回 ...

  7. Struts2【UI标签、数据回显、资源国际化】

    Struts2UI标签 Sturts2为了简化我们的开发,也为我们提供了UI标签...也就是显示页面的标签..... 但是呢,Struts2是服务端的框架,因此使用页面的标签是需要在服务器端解析然后再 ...

  8. 一脸懵逼学习Struts数据校验以及数据回显,模型驱动,防止表单重复提交的应用。

    1:Struts2表单数据校验: (1)前台校验,也称之为客户端校验,主要是通过Javascript编程的方式进行数据的验证. (2)后台校验,也称之为服务器校验,这里指的是使用Struts2通过xm ...

  9. HTML、jsp页面中radio,checkbox,select数据回显功能,默认被选中问题

    最近常常遇到各种复选框.单选框.下拉框的默认被选中的问题,开始也是绞尽脑汁的想办法,今天写一篇学习总结的博文来写一下学习总结. 单选框(radio)默认被选中: 一.jstl技术进行回显 <in ...

  10. Struts数据回显和模型驱动

    prams拦截器,可以把请求数据自动填充的action的属性中 举例1: JSP <input type=text name=userName /> <input type=text ...

随机推荐

  1. VC6.0 工程转到VS2010一些问题的描述及解决方法

    下列为VC6.0 工程转到VS2008一些问题的描述及解决方法 //////////////////////////////////////////////////////////////////// ...

  2. Mac系统的launchd、守护进程daemon(2013笔记整理)

    1. launchd Mac系统下通用的进程管理器,是Mac系统下非常重要的一个进程,一般来说该进程不允许直接以命令行的形式调用.只能通过其控制管理界面,launchctl来进行控制. launchd ...

  3. linux命令-stty

    一.用途: stty——改变和打印终端行设置 二.参数: 1.打印终端行设置 -a,--all   以人可读的方式打印所有当前设置:-a参数比单独的stty命令输出的终端信息更详细 -g,--save ...

  4. Django 之 JsonResponse 对象

    JsonResponse 是 HttpResponse 的子类,与父类的区别在于: JsonResponse 默认 Content-Type 类型为 application/json HttpResp ...

  5. 程序员笔记|常见的Spring异常分析及处理

    一.前言 相信我们每个人在SpringMVC开发中,都遇到这样的问题:当我们的代码正常运行时,返回的数据是我们预期格式,比如json或xml形式,但是一旦出现了异常(比如:NPE或者数组越界等等),返 ...

  6. iscsi使用教程(上)

    服务端 服务器环境 已经安装过qemu-img的32位ubuntu $ uname -a Linux ubuntu-virtual-machine 3.13.0-46-generic #76-Ubun ...

  7. OpenStack基础知识-打包知识点

    OpenStack是使用setuptools工具来进行打包,不过为了满足OpenStack项目的需求,引入了一个辅助工具pbr来配合setuptools完成打包工作. pbr (Python Buil ...

  8. 前端页面唯一字符串生成(Js)UUID

    function uuid() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 3 ...

  9. 洛谷P1607 [USACO09FEB]庙会班车Fair Shuttle

    P1607 [USACO09FEB]庙会班车Fair Shuttle 题目描述 Although Farmer John has no problems walking around the fair ...

  10. Mysql-7-mysql函数

    1.数学函数 用来处理数值数据方面的运算,主要的数学函数有:绝对值函数,三角函数,对数函数,随机函数.使用数学函数过程中,如果有错误产生,该函数会返回null值. 数学函数 功能介绍 组合键 abs( ...