springMVC 自定义类型转换器

DateLocalConvertor 这里我们起名

package com.itheima.convertor;


import org.springframework.core.convert.converter.Converter;  //Converter这里要使用得是 springframework 里得
import org.springframework.util.StringUtils;


import java.text.SimpleDateFormat;
import java.util.Date;
//Converter stirng 传入类型  Date 返回类型 完成转换
public class DateLocalConvertor implements Converter<String, Date> {
     

    //假设date 时间字符串格式为
    private String pattern = "yyyy-MM-dd";

    //自定义 pattern 时间格式
    public void setPattern(String pattern) {
     
        this.pattern = pattern;
    }

    @Override//  Converter 转换器 接口中得抽象方法  在此处对数据进行转换
    public Date convert(String s) {
     
        if(StringUtils.isEmpty(s)){
        //如果传入值为 null  或者 空字符串 停止转换
            return null ;
        }
        try {
     
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            return simpleDateFormat.parse(s);
        } catch (Exception e) {
     
            e.printStackTrace();
            throw new RuntimeException("格式转换失败");
        }
    }
}

webApplicationContext.xml 这是我们得spring 配置文件


<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">
    
    <context:component-scan base-package="com.itheima"/>
    
    <mvc:annotation-driven conversion-service="conversionService" />
    
    <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    bean>
    
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />

    
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <bean class="com.itheima.convertor.DateLocalConvertor">
                <property name="pattern" value="yyyy-MM-dd"/>
            bean>
        property>
    bean>
beans>

web.xml 配置

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:webApplicationContext.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
web-app>

前端部分

<form action="${pageContext.request.contextPath}/user/register" enctype="application/x-www-form-urlencoded" method="post">
    name : <input type="text" name="name" value="tom"><br/>
    password : <input type="password" name="password" value="123"><br/>
    birthday : <input type="date" name="birthday" value="1999-02-12"><br/>
  
    <input type="submit" value="提交">
form>

你可能感兴趣的:(老年健忘症笔记,springMVC,自定义类型转换器)