Why input ' 12 ' on the page, can copy to the handler method corresponding parameter?
And because a type converter is not able to convert a user-submitted string into all types required by the user, a custom type converter is required to
Case: Custom Date type converter requires format: YYYY/MM/DD
On the page:
<%--Created by IntelliJ. User:mycom Date:2018/3/ -Time : One: $ to change ThisTemplate Use File | Settings |File Templates.--%><%@ page contenttype="Text/html;charset=utf-8"Language="Java"Iselignored="false"%>"${pagecontext.request.contextpath}/first"Method="Post">User name:<input type="text"Name="name"Value="${name}"/>Age:<input type="text"Name=" Age"Value="${age}"/>Date of birth:<input type="text"Name="Birthday"/> <input type="Submit"Value="Submit"></form></body>Controller class
Package Demo16typeconverter;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 Javax.servlet.http.httpservletrequest;import java.util.Date;/** * Created by mycom on 2018/3/30.*/@Controller Public classTypecontroller {@ExceptionHandler PublicModelandview Dosome (Exception ex, httpservletrequest request) {Modelandview MV=NewModelandview (); Mv.addobject ("name", Request.getattribute ("name")); Mv.setviewname ("Login"); Mv.addobject ("ex", ex);//saved data, displaying error messages on the page with an El expression//To determine the type of exception if(ex instanceof Nameexception) {mv.setviewname ("nameexception"); } if(ex instanceof Ageexception) {mv.setviewname ("ageexception"); } returnMV; } @RequestMapping ("/first") PublicString Dofirst (string name,intAge , Date birthday) {System. out. println (name); System. out. println (age); System. out. println (birthday); return "Success"; }}
In the configuration file
<?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:mvc="Http://www.springframework.org/schema/mvc"Xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scanBase-package="Demo16typeconverter"></context:component-scan> <!--View Resolver-<beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"Value="/error/"></property> <property name="suffix"Value=". JSP"></property> </bean> <!--Configuring custom converters--<bean id="Myconverter" class="Demo16typeconverter.myconverter"></bean> <!--Register a converter-<bean id="Conversionservice" class="Org.springframework.context.support.ConversionServiceFactoryBean"> <property name="Converters" ref="Myconverter"></property> </bean> <!--notes Drive-<mvc:annotation-driven conversion-service="Conversionservice"></mvc:annotation-driven></beans>
Custom type Converters
Package Demo16typeconverter;import Org.springframework.core.convert.converter.converter;import Java.text.parseexception;import java.text.simpledateformat;import java.util.date;import Java.util.regex.Pattern;/** * Created by mycom on 2018/3/30.*/ Public classMyconverter Implements Converter<string,date> { PublicDate convert (String str) {SimpleDateFormat SDF=getconverter (str); Try{Date Date=sdf.parse (str); returndate; } Catch(ParseException e) {e.printstacktrace (); } return NULL; } PrivateSimpleDateFormat getconverter (String str) {SimpleDateFormat SDF=NewSimpleDateFormat ("yyyy mm month DD Day");
Match a different formatif(Pattern.matches ("^\\d{4}-\\d{2}-\\d{2}$", str)) {SDF=NewSimpleDateFormat ("YYYY-MM-DD"); }Else if(Pattern.matches ("^\\d{4}/\\d{2}/\\d{2}$", str)) {SDF=NewSimpleDateFormat ("YYYY/MM/DD"); }Else if(Pattern.matches ("^\\d{4}\\d{2}\\d{2}$", str)) {SDF=NewSimpleDateFormat ("YYYYMMDD"); } returnSDF; }}
Configuration files and pages are still used in one of the above
SPRINGMVC (14) type converter