当是非常规的数据类型,无法进行类型转换,会抛出异常
EditorController:
package com.baizhiedu;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;
@Controller
@RequestMapping("/editor")
public class EditorController {
@RequestMapping("/editor1")
public String editor1(String name, Date birthday){
System.out.println("name="+name);
System.out.println("birthday="+birthday);
return "param1";
}
}
editor.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/4
Time: 8:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/editor/editor1">
UserName:<input type="text" name="name"><br>
Birthday:<input type="text" name="birthday"><br>
<input type="submit" value="reg">
</form>
</body>
</html>
自定义类型转换器:
定义转换器:DateConverter:转换日期格式
package com.baizhiedu;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//自定义类型转换器
public class DateConverter implements Converter<String, Date> {//第一个参数String是传进来的类型,Date要转换的类型
@Override
public Date convert(String source) {
Date parse=null;
try {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
parse=simpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
}
在配置文件中配置:1.2.3
dispatcher.xml:
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--设置注解扫描的路径 -->
<context:component-scan base-package="com.baizhiedu"/>
<!--引入springMVC的核心功能-->
<!--3. 添加一个属性onversion-service使用我们创建的转换器-->
<mvc:annotation-driven conversion-service="serviceFactoryBean"/>
<!--视图解析器配置: 控制器方法返回的结果的前缀和后缀-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--1. 创建的类型转化器-->
<bean id="dateConverter" class="com.baizhiedu.DateConverter"></bean>
<!--2. 配置ForamattingConersionServiceFactoryBean-->
<bean id="serviceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!--把上面创建的类型转换器注册到ForamattingConersionServiceFactoryBean-->
<property name="converters">
<set>
<ref bean="dateConverter"/>
</set>
</property>
</bean>
</beans>