SpringMVC自定义绑定参数(解决参数解析错误问题)

SpringMVC自定义绑定参数

背景:因为在前段发送日期类型的数据的时候,日期的格式有很多,但是SpringMVC在前端控制器(Controller)中的Date类型参数只能够识别并转换格式为"yyyy-mm-dd"这种格式的日期类型,所以在设置日期的时候需要自定义一种日期格式。

步骤:

1.创建转换器

package com.helong.web.convert;

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

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

//转换接收参数为日期类型的方法
public class DateConvert implements Converter<String, Date> {
    @Override
    public Date convert(String s) {
        if(s != null){
            //设置转换参数的格式(将一个字符串类型转换为一个日期格式)
            //设置之后就不能使用之前的数据格式了,如果不是按照下面的格式进行输入的话就会报错
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
            try {
                return simpleDateFormat.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

2.在SpringMVC核心配置文件中自定义转换器(转换工厂和注解扫描)

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!--开启注解扫描-->
    <context:component-scan base-package="com.helong"/>

    <!--视图解析器前/后缀设置-->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        &lt;!&ndash;设置前缀&ndash;&gt;
        <property name="prefix" value="/WEB-INF/helong/"/>
        &lt;!&ndash;设置后缀&ndash;&gt;
        <property name="suffix" value=".jsp"/>
    </bean>-->


    <!--配置Converter转换器  转换工厂-->
    <bean id="dataConvert" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!--可以配置多个转换器-->
        <property name="converters">
            <list>
                <bean class="com.helong.web.convert.DateConvert"></bean>
            </list>
        </property>
    </bean>

    <!--注解驱动-->
    <mvc:annotation-driven conversion-service="dataConvert"/>
    
</beans>

 3.在前端控制器中接收参数

package com.helong.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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

@Controller
public class MyController2 {

    /*接收日期类型的参数的时候2010/01/01这种格式不会发生错误
    * 但是由于日期有很多格式,springmvc没有办法把其他类型字符串转换成日期格式。所以需要自己自定义参数绑定。
    * 解决方法:自定义类型转换器
    * 1.创建转换器
    * 2.在springmvc核心配置文件中自定义转换器
    * */
    @RequestMapping("/convert")
    public ModelAndView myform1(Integer age, Date birthday,String name ){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(age);
        //将接收到的日期格式的数据装换为自己想要的日期的字符串格式的方法
        System.out.println(new SimpleDateFormat("yyyy-mm-dd").format(birthday));
        System.out.println(name);
        modelAndView.setViewName("/second.jsp");
        return modelAndView;
    }

}

注:SpringMVC的目录结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值