• springmvc请求参数类型转换器和原生api


    一、springmvc的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
            https://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.wuxi"></context:component-scan>
        <!--视图解析器-->
        <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="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
                <set>
                    <bean class="com.wuxi.utils.StringToDateConverter"></bean>
                </set>
            </property>
        </bean>
        <!--开启springmvc框架注解的支持-->
        <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    </beans>

    二、转换的类

    package com.wuxi.utils;
    
    import org.springframework.core.convert.converter.Converter;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class StringToDateConverter implements Converter<String, Date> {
        @Override
        public Date convert(String string) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = sdf.parse(string);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
    }

    三、接口

    @RequestMapping("/student")
    public String student(Student student, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(student);
        return "success";
    }
  • 相关阅读:
    图片验证码, 登录, 注销, 修改密码
    注册页面及注册功能实现
    高级配置文件, csrf, django settings源码, django auth模块, 文件配置的插拔式设计
    cookie操作, session操作, django中间件
    半自动创建多对多关系表, forms组件
    sweetalert, bulk_create, 分页器
    orm查询优化, MVC与MTV, choices参数, ajax
    聚合查询, 分组查询, F与Q查询, 常见字段及参数, 自定义Char字段, 事务操作
    Contest2058
    ACM版《孔乙己》
  • 原文地址:https://www.cnblogs.com/linding/p/13672421.html
Copyright © 2020-2023  润新知