首页 > 其他分享 >Spring 自定义propertyEditor

Spring 自定义propertyEditor

时间:2022-12-13 10:46:15浏览次数:59  
标签:province city String 自定义 Spring propertyEditor Address address public

image

image

User

package com.example.zylspringboot.selfEditor;

public class User {

    private String name;
    private Address address;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", age=" + age +
                '}';
    }
}

Address

package com.example.zylspringboot.selfEditor;

public class Address {

    private String province;

    private String city;

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + '}';
    }
}

SelfPropertyEditor

package com.example.zylspringboot.selfEditor;

import java.beans.PropertyEditorSupport;

public class SelfPropertyEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        String[] s = text.split("_");
        Address address = new Address();
        address.setCity(s[0]);
        address.setProvince(s[1]);
        super.setValue(address);
    }
}

AcaakPropertyRegistor

package com.example.zylspringboot.selfEditor;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;

public class AcaakPropertyRegistor implements PropertyEditorRegistrar {
    @Override
    public void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
        propertyEditorRegistry.registerCustomEditor(Address.class,new SelfPropertyEditor());
    }
}

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:context="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="user" class="com.example.zylspringboot.selfEditor.User">
        <property name="age" value="18"></property>
        <property name="name" value="Acaak"></property>
        <property name="address" value="广东省_广州市"></property>
    </bean>

    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="propertyEditorRegistrars">
            <list>
                <bean class="com.example.zylspringboot.selfEditor.AcaakPropertyRegistor"></bean>
            </list>
        </property>
    </bean>
	或
	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="com.example.zylspringboot.selfEditor.Address">
                    <value>com.example.zylspringboot.selfEditor.SelfPropertyEditor</value>
                </entry>
            </map>
        </property>
    </bean>
</beans>

标签:province,city,String,自定义,Spring,propertyEditor,Address,address,public
From: https://www.cnblogs.com/Acaak/p/16977888.html

相关文章

  • 【Emoji】Spring 通过注解 处理 转换 Emoji
      依赖<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>4.5.6</version></dependency><dependency>......
  • echarts 图片自定义下载、导出
    来自:https://www.jianshu.com/p/90bf22936439侵删除   //图表导出-自己的方法toolbox:{show:true,feature:{sa......
  • Spring Boot命令指定环境启动jar包
    原文地址:SpringBoot命令指定环境启动jar包-Stars-One的杂货小窝记下通过命令行的方式去改变springboot项目中的环境配置信息命令项目中有以下配置application.yml......
  • spring boot 拦截器
    springboot使用拦截器1.创建拦截器类,继承HandlerInterceptor2.注册拦截器,指定拦截规则springframework中的拦截器类需要继承与HandlerInterceptor,springboot也是......
  • SpringBoot引入外部jar包,并打包到项目jar包中
    SpringBoot引入外部jar包,并打包到项目jar包中在网上下载需要的jar包,在项目中新建文件夹将jar包放进去通过File->ProjectStructure引入选中jar包加载成功后,ja......
  • AntD中Form表单自定义验证规则
    <Form.Item label="学生编码" name="stuId" validateFirst={true}//出错时两个校验分别生效 rules={[ {required:true,message:'学生编码不能为空'}, {val......
  • Springboot多数据源使用示例
    1、配置文件spring.datasource.url=jdbc:mysql://198.168.1.1:3306/user?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&rewriteBatchedStatements=true&t......
  • springboot 简单设置mysql用户名密码加密
     如何将yml文件中暴露的数据库用户名和密码由明文改为密文,提高安全性。个人觉得是最简单的方式实现yml代码,用户名密码使用文章后面提供的加密算法或者自行寻找方法spr......
  • Unity 自定义创建脚本模板
    Unity自定义创建脚本模板原理:以模板代码为底板,通过关键字替换来实现代码创建两种实现方案方案11.先准备好对应的代码模板,放到Assets\ScriptTemplates目录下usingSys......
  • SpringBoot 自动装配的原理分析
    关于SpringBoot的自动装配功能,相信是每一个Java程序员天天都会用到的一个功能,但是它究竟是如何实现的呢?今天阿粉来带大家看一下。自动装配案例首先我们通过一个案例......