SpringMVC ——自定义类型转换器代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--请求参数绑定--%>
  <%--  <a href="param/testParam?username=fy&password=fy">RequestMapping注解</a>

    <form action="param/saveAccount" method="post">
        姓名:<input type="text" name="username"/>
        密码:<input type="text" name="password"/>
        金额:<input type="text" name="money"/>
        用户名:<input type="text" name="user.name"/>
        年龄:<input type="text" name="user.age"/>
        <input type="submit" value="提交"/>
    </form>
    --%>

    <!--list map参数绑定-->
 <%--   <form action="param/saveAccount" method="post">
        姓名:<input type="text" name="username"/>
        密码:<input type="text" name="password"/>
        金额:<input type="text" name="money"/>

        List用户名:<input type="text" name="list[0].name"/>
        List年龄:<input type="text" name="list[0].age"/>

        Map用户名:<input type="text" name="mao['fy'].name"/>
        Map年龄:<input type="text" name="mao['fy'].age"/>
        <input type="submit" value="提交"/>
    </form>
    --%>

    <!--自定义类型转换器代码编写-->
    <form action="param/saveUser" method="post">
        姓名:<input type="text" name="name"/>
        年龄:<input type="text" name="age"/>
        生日:<input type="text" name="date"/>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>

package com.fy.domain;

import java.util.Date;

public class User {
    private String name;
    private int age;
    private Date date;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", date=" + date +
                '}';
    }
}

package com.fy.utils;

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        if(source==null){
            throw new RuntimeException("请您传入数据");
        }
        DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
        try {
            return  df.parse(source);
        } catch (Exception e) {
            throw new RuntimeException("数据类型转换出现错误");
        }
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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-scan base-package="com"/>
    <!--视图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--配置自定义类型转换器-->
    <bean id="conversionServiceFactory" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.fy.utils.StringToDateConverter"></bean>
            </set>
        </property>
    </bean>
    <!--开启SpringMVC注解的支持-->
    <mvc:annotation-driven conversion-service="conversionServiceFactory"/>
</beans>

control:
在这里插入图片描述

发布了25 篇原创文章 · 获赞 70 · 访问量 3214

猜你喜欢

转载自blog.csdn.net/qq_44706044/article/details/104190816