springMVC 日期转换器

package com.pc.helmet.controller;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * @ClassName: BaseCl
 * @Description: TODO
 * @author: TimBrian
 * @date: 2019年8月14日 上午11:33:19
 */
@Controller
public class BaseCl {


    /**
     * @author TimBrian
     * @since 2019年8月14日上午11:33:45
     * @param view 获得一个视图
     * @return 视图路径
     * @throws
     */
    public String getView(final String view) {
        return view;
    }

    /**
     * @Description 日期类型转换器
     * @param dataBinder 数据Binder
     */
    @InitBinder
    public void initDateBinder(final WebDataBinder dataBinder) {
        // 日期转换
        dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public String getAsText() {
                if (getValue() != null) {
                    return new SimpleDateFormat("yyyy-MM-dd").format((Date) getValue());
                } else {
                    return null;
                }
            }

            @Override
            public void setAsText(final String value) {
                try {
                    if (value != null && !value.isEmpty()) {
                        setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
                    } else {
                        setValue(null);
                    }
                } catch (final java.text.ParseException e) {
                    e.printStackTrace();
                }
            }
        });
        // 字符串去空格
        dataBinder.registerCustomEditor(String.class, new PropertyEditorSupport() {
            @Override
            public String getAsText() {
                return (String) getValue();
            }

            @Override
            public void setAsText(final String value) {
                if (value != null) {
                    setValue(value.trim());
                } else {
                    setValue(null);
                }
            }
        });
    }

    /**
     * 重定向到一个url
     *
     * @param url
     * @return
     */
    protected String sendRedirect(final String url) {
        return "redirect:" + url;
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值