1.配置web.xml文件html
<!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.自定義日期變流器java
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.日期格式處理器web
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.映射器spring
<?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:註冊頁面spring-mvc
<%@ 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:成功頁面mvc
<html> <body> <h2>Hello World!</h2> </body> </html>
測試結果1:app
測試結果2:jsp
測試結果3:post
測試結果4:測試