自定义类型转换器

前端代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>自定义类型转换器</title>
</head>
<body>
<%--自定义类型转换器--%>
<form action="param/saveuser" method="post">
    曾用名:<input type="text" name="uname" placeholder="请输入您的姓名"><br>
    年龄:<input type="text" name="age" placeholder="请输入您的年龄"><br>
    用户生日:<input type="text" name="date"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

控制器代码: 

package Controller;

import domain.Account;
import domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 请求参数绑定
 */

@Controller
@RequestMapping("/param")
public class paramController {
    /**
     * 自定义类型转换器
     * @param user
     * @return
     */
    @RequestMapping("/saveuser")
    public String saveuser (User user){
        System.out.println("执行了......");
        System.out.println(user);
        return "success" ;
    }
}

把字符串转换日期类:

package 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 StringToDataConverter implements Converter<String, Date> {

    /**
     *
     * @param s
     * @return
     */
    @Override
    public Date convert(String s) {
        //判断
        if (s==null){
            throw new RuntimeException("请您传入数据") ;
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd") ;
        //把字符串转换日期
        try {
            return df.parse(s) ;
        } catch (Exception e) {
            throw new RuntimeException("数据类型转换出现错误....") ;
        }
    }
}

springmvc文件配置自定义类型转换器

要在<mvc:annotation-driven"/>进行注册

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
			    http://www.springframework.org/schema/aop/spring-aop.xsd
			    http://www.springframework.org/schema/tx
			    http://www.springframework.org/schema/tx/spring-tx.xsd
			    http://www.springframework.org/schema/mvc
			    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="Controller"/>
<!--视图解析器-->
    <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 配置自定义类型转换器 -->
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="utils.StringToDataConverter"></bean>
            </set>
        </property>
    </bean>

    <!--配置映射器处理器,处理器适配器-->
    <mvc:annotation-driven conversion-service="conversionService"/>

    <!--释放静态资源-->
    <mvc:default-servlet-handler/>

</beans>

省略web.xml文件..........

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值