SpringMVC09 Converter变流器、数据回显、异常测试
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变流器、数据回显、异常测试的更多相关文章
- SpringMVC由浅入深day02_5数据回显_6异常处理器
5 数据回显 5.1 什么数据回显 表单提交失败需要再回到表单页面重新填写,原来提交的数据需要重新在页面上显示. 5.2 pojo数据回显方法 1.springmvc默认对pojo数据进行回显. po ...
- 表单很多数据项录入的时候,提交controller发生异常,数据回显。
1.添加的情况(Model传递Form Data) request.getSession().setAttribute("car", car); //抛出异常的时候,数据回显. 2 ...
- SpringMVC【参数绑定、数据回显、文件上传】
前言 本文主要讲解的知识点如下: 参数绑定 数据回显 文件上传 参数绑定 我们在Controller使用方法参数接收值,就是把web端的值给接收到Controller中处理,这个过程就叫做参数绑定.. ...
- SpringMVC学习(四)———— 数据回显与自定义异常处理器
一.数据回显技术 Springmvc默认支持对pojo类型的数据回显,默认不支持简单类型的数据回显 1.1.什么是数据回显? 在信息校验时,如果发生校验错误,那么把校验的数据信息,依然停留在当前页面, ...
- springmvc(五) 数据回显与自定义异常处理器
这章讲解一下springmvc的数据回显和自定义异常处理器的使用,两个都很简单 --WH 一.数据回显技术 Springmvc默认支持对pojo类型的数据回显,默认不支持简单类型的数据回显 1.1.什 ...
- SpringMVC第五篇【方法返回值、数据回显、idea下配置虚拟目录、文件上传】
Controller方法返回值 Controller方法的返回值其实就几种类型,我们来总结一下-. void String ModelAndView redirect重定向 forward转发 数据回 ...
- Struts2【UI标签、数据回显、资源国际化】
Struts2UI标签 Sturts2为了简化我们的开发,也为我们提供了UI标签...也就是显示页面的标签..... 但是呢,Struts2是服务端的框架,因此使用页面的标签是需要在服务器端解析然后再 ...
- 一脸懵逼学习Struts数据校验以及数据回显,模型驱动,防止表单重复提交的应用。
1:Struts2表单数据校验: (1)前台校验,也称之为客户端校验,主要是通过Javascript编程的方式进行数据的验证. (2)后台校验,也称之为服务器校验,这里指的是使用Struts2通过xm ...
- HTML、jsp页面中radio,checkbox,select数据回显功能,默认被选中问题
最近常常遇到各种复选框.单选框.下拉框的默认被选中的问题,开始也是绞尽脑汁的想办法,今天写一篇学习总结的博文来写一下学习总结. 单选框(radio)默认被选中: 一.jstl技术进行回显 <in ...
- Struts数据回显和模型驱动
prams拦截器,可以把请求数据自动填充的action的属性中 举例1: JSP <input type=text name=userName /> <input type=text ...
随机推荐
- Spring Boot 专栏
http://blog.csdn.net/column/details/spring-boot.html?&page=2
- 谁是Docker的开发者
由CHRIS DAWSON发表在thenewstack/DATA RESEARCH qianhen123/CHB译 我们分析了Docker的容器库并提出两个问题: 1.Docker的贡献者们感兴趣的其 ...
- [原创]Devexpress XtraReports 系列索引
该系列已经完结...以后如果有高级功能,会再开一个新的系列,该系列是比较基础的报表应用..谢谢大家一直的支持. [原创]Devexpress XtraReports 系列 1 创建静态报表 Demo地 ...
- 项目一:项目第二天 Jquery ztree使用展示菜单数据 2、 基础设置需求分析 3、 搭建项目框架环境--ssh(复习) 4、 SpringData-JPA持久层入门案例(重点) 5、 Easyui menubutton菜单按钮使用 6、 Easyui messager消息框使用
1. Jquery ztree使用展示菜单数据 2. 基础设置需求分析 3. 搭建项目框架环境--ssh(复习) 4. SpringData-JPA持久层入门案例(重点) 5. Easyui menu ...
- 文件格式——gff格式
Gff文件格式 gff格式是Sanger研究所定义,是一种简单的.方便的对于DNA.RNA以及蛋白质序列的特征进行描述的一种数据格式,已经成为序列注释的通用格式,比如基因组的基因预测,许多软件都支持输 ...
- python笔记——均值、方差、中位数计算
from __future__ import print_function # 均值计算 data = [3.53, 3.47, 3.51, 3.72, 3.43] average = float(s ...
- winDump
windump -i 00-00-10-00-43-A2 监听网卡(一个适配器一个网卡,一个mac)
- JavaScript学习系列7 -- JavaScript中的运算符
今天,我们来说一说JavaScript中的运算符,首先我们来讲一讲 一元运算符 JavaScript中的一元运算符有以下几种 1. delete delete 运算符主要用于删除对以前定义的对象属性或 ...
- CSS学习系列3--CSS3中的box-shadow的使用
在CSS中,text-shadow是给文本添加阴影效果. box-shadow则是给元素块添加周边阴影效果. box-shadow基本的语法形式如下 box-shadow: [inset] x-off ...
- Umbraco back office 中templates显示不出来问题解决 (一片空白)
在公司一个项目中,遇到一个问题,登录Umbraco back office,该项目的settings => Templates 已经有该项目的10几个view (templates), 但是,点 ...