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: