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 ...
随机推荐
- <正则吃饺子>:关于java中垃圾回收技术的简单学习总结
知识介绍来自网络,后面会根据继续学习进行补充和适当的修改,谢谢!原文地址:http://www.importnew.com/26821.html#comment-578355 java中的垃圾回收机制 ...
- (cdh)hive 基础知识 名词详解及架构
过程 启动 hive 之后出现的 CLI 是查询任务的入口,CLI 提交任务给 Driver Driver 接收到任务后调用 Compiler,Executor,Optimizer 将 SQL 语句转 ...
- net.sf.json-lib maven依赖问题.
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</art ...
- 利用java mail发送邮件
import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import java ...
- ORACLE AWR报告生成步骤
ORACLE AWR报告生成步骤 (以PL/SQL中命令窗口为例) 1.sqlplus或plsql的commod窗口(命令窗口)运行命令 @D:\oracle\product\10.2.0\db_1\ ...
- python fabric的安装与使用
背景:fabric主要执行远程的shell命令,包括文件的上传.下载,以及提示用户输入等辅助其它功能. 测试系统:ubuntu16 要求:python //系统有自带,ubuntu16 通常自带pyt ...
- socket消息发送
expressClient.html <html><head><meta http-equiv="Content-Type" content=&quo ...
- AngularJs(Part 7)--Build-in Directives
Directives In AngularJS, we can use a variety of naming conventions to reference directives . In the ...
- CF1042E Vasya and Magic Matrix
感觉不会期望. 首先把所有格子按照权值从小到大排一下序,这样一共有$n * m$个元素,每个元素有三个属性$x, y, val$. 下文中的下标均为排序后的下标. 这样子我们就可以推出公式: $f_i ...
- 使用form 组件写一个用户注册,并用 bootstrap渲染
需求:使用form组件,写一个用户注册系统,包含用户名, 密码, 确认密码,手机号,性别,爱好,注册.并用bootsrap渲染,成果如下: 首先创建一个django 项目.然后在连接pymysql数据 ...