zoukankan      html  css  js  c++  java
  • Spring MVC 自定义类型转换器

    新建一个自定义转换器

    import org.springframework.core.convert.converter.Converter;
    import org.springframework.stereotype.Component;
    
    import com.atguigu.springmvc.crud.entities.Department;
    import com.atguigu.springmvc.crud.entities.Employee;
    
    @Component
    public class EmployeeConverter implements Converter<String, Employee> {
    
        @Override
        public Employee convert(String source) {
            if(source != null){
                String [] vals = source.split("-");
                //GG-gg@atguigu.com-0-105
                if(vals != null && vals.length == 4){
                    String lastName = vals[0];
                    String email = vals[1];
                    Integer gender = Integer.parseInt(vals[2]);
                    Department department = new Department();
                    department.setId(Integer.parseInt(vals[3]));
                    
                    Employee employee = new Employee(null, lastName, email, gender, department);
                    System.out.println(source + "--convert--" + employee);
                    return employee;
                }
            }
            return null;
        }
    
    }

    配置xml

      <!-- 配置 ConversionService -->
        <bean id="conversionService"
            class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
            <property name="converters">
                <set>
                    <ref bean="employeeConverter"/>  <!--  自定义的类名,首字母小写 -->
                </set>
            </property>    
        </bean>

      <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
  • 相关阅读:
    cacti (可以利用yum安装cacti的配置)
    zabbix
    HA集群
    DNS搭建
    MySQL主从配置
    解析jsp的 tomcat 、resin
    samba
    squid 代理服务
    Application binary interface and method of interfacing binary application program to digital computer
    Pyqt4的事件与信号
  • 原文地址:https://www.cnblogs.com/eason-d/p/9249034.html
Copyright © 2011-2022 走看看