converter 工具类的写法和配置,常用的工具类,UUID

在这里插入图片描述

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 source) {
        //日期转换方法,把字符串,转换为时间类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date dt = sdf.parse(source);
            return dt;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

在springMVC配置文件里,写类型转换工厂bean,并且注解引用
在这里插入图片描述

<?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
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--扫描包:创建web层对象-->
    <context:component-scan base-package="com.itheima.controller"></context:component-scan>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--注解驱动,并且引用工厂类对象-->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

    <!--静态资源放行-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!--<mvc:resources mapping="/css/*" location="/css/"></mvc:resources>-->
    <!--<mvc:resources mapping="/img/*" location="/img/"></mvc:resources>-->
    <!--<mvc:resources mapping="/plugins/*" location="/plugins/"></mvc:resources>-->

    <!--创建类型转换器工厂类对象-->
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.itheima.converter.StringToDateConverter"></bean>
            </set>
        </property>
    </bean>
</beans>

UUID:

public class UUIDUtil {
	public static String getId(){
		//得到随机的32为字符串,去掉中间的 - ,并把所有字符转换为大写
		String s = UUID.randomUUID().toString().replace("-", "").toUpperCase();
		return s;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值