SpringMVC:自定义类型转换器

当是非常规的数据类型,无法进行类型转换,会抛出异常

 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>

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值