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. WCF服务用户名密码访问

    有2种方式, 第一直接在程序中指定用户名密码,配置调用 private void BtnSearch_Click(object sender, EventArgs e) { try { var cli ...

  2. linux日常管理-系统进程查看工具-ps

    查看系统有那些进程 命令有ps aux 和命令 ps -elf USER  哪个用户使用了这个进程 PID  进程的id %CPU 占用CPU的百分比 %MEM 占用内存的百分比 VSZ 虚拟内存的大 ...

  3. Intent的简单概述

    Intent是负责在系统组件之间传递信息的一个对象,就像名字一样,是一个意图,可以将当前组件的意图传递给系统,例如启动Activity等,还可以在传递的时候附加上一些值,可以用Bundle对象封装这些 ...

  4. 关于UI性能优化

    1.使用已经有的VIEW,而不是每次都去新生成一个 2.创建自定义类来进行组件和数据的缓存,在下一次调用的时候直接从FLAG中取出 3.分页,预加载 使用VIEWSTUB进行调用时加载 VIEWSTU ...

  5. C#内存管理之托管堆与非托管堆( reprint )

    在 .NET Framework 中,内存中的资源(即所有二进制信息的集合)分为“托管资源”和“非托管资源”.托管资源必须接受 .NET Framework 的 CLR (通用语言运行时)的管理(诸如 ...

  6. 详解Java中的Object.getClass()方法

    详解Object.getClass()方法,这个方法的返回值是Class类型,Class c = obj.getClass(); 通过对象c,我们可以获取该对象的所有成员方法,每个成员方法都是一个Me ...

  7. python下一个转码的问题

    我想把一个quoted的字符串经过unquote处理后,打印出来.被unquote处理后的字串应该是utf-8的,因此还需要按照utf-8再做一次解码,代码如下:   import urllib im ...

  8. php5.6安装window7安装memcache.dll库所遇到的误区

    问题: window7 64位,下载的库 memcache.dll 为64位的,且对应php的版本.但是重启后phpstudy查看phpinfo依然没有memcache: 根源: 发现是下载的 mem ...

  9. 通过增删改查对比Array,Map,Set,Object的使用成本和实现方式

    1.Array 和 Map 对比 { // array and map 增 查 改 删 let map = new Map(); let arr = []; // 增 map.set('a', 1); ...

  10. 51nod1307(暴力树剖/二分&dfs/并查集)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...