SSM-SpringMVC-30:SpringMVC中InitBinder的骇客级优化

 

 

 

 ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

 

 

上篇博客利用initbinder做了局部的日期类型转换,但是兼容性不要,只支持yyyy-MM-dd这种,所以我们这里进行进一步的优化

其实话说回来了,要想限定格式做最稳定的日期类型转换,就是用日期控件,让用户选,你通过js生成日期数据,这可以省好多麻烦

 

案例开始:

  1.定义一个自己的日期编辑类,继承PropertiesEditor

 

package cn.dawn.day22initbinder.editor;

import org.springframework.beans.propertyeditors.PropertiesEditor;

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

/**
 * Created by Dawn on 2018/3/31.
 */
public class MyDateEdlitor extends PropertiesEditor {
    @Override
    /*网线打过来的长的像日期的字符串str*/
    public void setAsText(String str) throws IllegalArgumentException {
        SimpleDateFormat sdf=getSimpleDate(str);
        Date date=null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        setValue(date);
    }


    private SimpleDateFormat getSimpleDate(String str) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        if(Pattern.matches("^\\d{4}-\\d{1,2}-\\d{2}$",str)){
            sdf=new SimpleDateFormat("yyyy-MM-dd");
        }
        if(Pattern.matches("^\\d{4}/\\d{1,2}/\\d{2}$",str)){
            sdf=new SimpleDateFormat("yyyy/MM/dd");
        }
        if(Pattern.matches("^\\d{4}\\d{1,2}\\d{2}$",str)){
            sdf=new SimpleDateFormat("yyyyMMdd");
        }
        return sdf;
    }
}

 

  2.自定义处理器和方法

 

package cn.dawn.day22initbinder;

import cn.dawn.day22initbinder.editor.MyDateEdlitor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * Created by Dawn on 2018/3/31.
 */

@Controller
public class MultiController {
    /*@InitBinder*/
    @InitBinder("birthday")
    public void initBinder(WebDataBinder binder){
        binder.registerCustomEditor(Date.class,new MyDateEdlitor());
    }


    /*initbinder*/
    @RequestMapping("/multiinitbinderFirst")
    public String multiinitbinderFirst(String username, Integer userage, Date birthday) throws Exception {
        System.out.println("2222222222");
        System.out.println(username);
        System.out.println(userage);
        System.out.println(birthday);
        return "success";
    }
}

 

  3.和上篇博客一样的大配置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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

    <!--包扫描器-->
    <context:component-scan base-package="cn.dawn.day22initbinder"></context:component-scan>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/day22/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>


</beans>

 

  4.修改web.xml的中央调度器的上下文配置位置为上面这个xml文件

  5.jsp页面:

    5.1login.jsp

 

<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>登录</h2>
<form action="${pageContext.request.contextPath}/multiinitbinderFirst" method="post">

    用户名:<input name="username" value="${username}">
    年龄:<input name="userage">
    生日:<input name="birthday">

    <input type="submit" value="登录"/>
</form>
</body>
</html>

 

    5.2success.jsp

 

<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
<html>
<body>
<%--<img src="image/1.jpg">--%>
<h2>Success!</h2>
</body>
</html>

 

  6.启动tomcat,访问login.jsp

 

转载于:https://www.cnblogs.com/DawnCHENXI/p/8684075.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值