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. c语言基础 c和指针

    句子 c规定数组名代表数组首元素的地址 如果&a 则代表整个数组 没有内存哪来的指针 数据类型的本质:固定大小内存的别名 变量的本质:(一段连续)内存空间的别名,内存空间的标号 指针是一种数据 ...

  2. 调试opencv调用摄像头程序时碰到的问题

    昨天晚上想把opencv学习笔记整理一下,当跑opencv调用摄像头的程序的时候老是出现Assertion failed (size.width>0 && size.height ...

  3. Java学习路线-知乎

    鼬自来晓 378 人赞同 可以从几方面来看Java:JVM Java JVM:内存结构和相关参数含义 · Issue #24 · pzxwhc/MineKnowContainer · GitHub J ...

  4. JS设置cookie、读取cookie、删除cookie(转)

    JS设置cookie.读取cookie.删除cookie 转载  2015-04-17   投稿:hebedich    我要评论 Js操作Cookie总结(设置,读取,删除),工作中经常会用到的哦! ...

  5. Blast 如何使用Blast+(Linux)转载

    下载数据 #此处下载对应物种的数据库ftp://ftp.ncbi.nih.gov/genomes/,下载fna格式的即可   wget ftp://ftp.ncbi.nih.gov/genomes/A ...

  6. ZOJ 3946 Highway Project (最短路)

    题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...

  7. 第二周作业-影评、靶机和攻击机的安装与配置、kali的配置、DNS解析

    教材作业 第一章作业一 <黑客军团>第2季第1集影评 本文只分析与黑客攻击有关的情节,不谈其他. 开头,男主通过ssh以root身份远程连接到了一台服务器,并在其上执行了名为fuxsocy ...

  8. Python开发【第二篇】: 基本数据类型(一)

    1. 整型   整型即整数,用 int 表示,在 Python3 中整型没有长度限制. 1.1 内置函数   1. int(num, base=None)   int( ) 函数用于将字符串转换为整型 ...

  9. 消息队列--RabbitMQ(二)

    1.常用的几种队列简介 RabbitMQ有五种常用的队列,分别是:简单队列.work模式.发布订阅模式.路由模式.主题(Topic)模式.其实发布订阅.路由.主题这三种模式都从属于与routingke ...

  10. 利用superlance监控supervisor运行状态

    此文已由作者张家裕授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近开发问到supervisor管理下的进程重启了,有无办法做到主动通知,楼主最先想到的是superviso ...