首页 > 其他分享 >自定义属性编辑器

自定义属性编辑器

时间:2023-09-23 21:44:58浏览次数:36  
标签:city String 自定义 import 编辑器 provice org public 属性

1,编写实体类

Address.java

package com.mashibing.selfEditor;

public class Address {
private String provice;
private String city;
private String town;

public String getProvice() {
return provice;
}

public void setProvice(String provice) {
this.provice = provice;
}

public String getCity() {
return city;
}

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

public String getTown() {
return town;
}

public void setTown(String town) {
this.town = town;
}

@Override
public String toString() {
return "Address{" +
"provice='" + provice + '\'' +
", city='" + city + '\'' +
", town='" + town + '\'' +
'}';
}
}
Customer.java
2、定义属性编辑器
AddressPropertyEditor.java
package com.mashibing.selfEditor;

import java.beans.PropertyEditorSupport;

public class AddressPropertyEditor extends PropertyEditorSupport {

@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] s=text.split("_");
Address address=new Address();
address.setProvice(s[0]);
address.setCity(s[1]);
address.setCity(s[2]);
this.setValue(address);
}
}
3、定义属性编辑注册器
AddressPropertyEditorRegistrar.java
package com.mashibing.selfEditor;

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

public class AddressPropertyEditorRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
propertyEditorRegistry.registerCustomEditor(Address.class,new AddressPropertyEditor());
}
}
4、配置文件
selfEditor.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="customer" class="com.mashibing.selfEditor.Customer">
<property name="name" value="qiangShan"></property>
<property name="address" value="江西省_抚州市_南城县"></property>
</bean>

<!-- <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<bean id="addressPropertyEditorRegistrar" class="com.mashibing.selfEditor.AddressPropertyEditorRegistrar"></bean>
</list>
</property>
</bean>-->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.mashibing.selfEditor.Address">
<value>com.mashibing.selfEditor.AddressPropertyEditor</value>
</entry>
</map>
</property>
</bean>
</beans>
5、测试类
TestSelfEditor.java
import com.mashibing.selfEditor.Customer;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSelfEditor {

@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("selfEditor.xml");
Customer customer = context.getBean("customer", Customer.class);
System.out.println(customer);
}
}

标签:city,String,自定义,import,编辑器,provice,org,public,属性
From: https://www.cnblogs.com/shanqiang1/p/17725111.html

相关文章

  • 自定义标签完成解析工作
    1、创建实体类User.javapackagecom.mashibing.selftag;publicclassUser{privateStringusername;privateStringemail;privateStringpassword;publicUser(){}publicUser(Stringusername,Stringemail,Stringpassword){this.use......
  • jar包运行时提示jar中没有主清单属性的解决
    在pom文件中添加<build><!--打包成jar包时的名字--><finalName>xxxxx</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifac......
  • elasticsearch 自定义字典
    ......
  • Vue-插槽及自定义事件分发
    一.插槽slot在某些场景中,我们可能想要为子组件传递一些模板片段,让子组件在它们的组件中渲染这些片段。<slot> 元素是一个插槽出口 (slotoutlet),标示了父元素提供的插槽内容 (slotcontent)将在哪里被渲染。 插槽就好比一个占位符,它不是解决页面必须元素的位置,而是解决未......
  • 运行 jar 命令提示没有主清单属性
    运行jar命令提示没有主清单属性参考文档:解决:运行jar命令提示没有主清单属性-码农教程(manongjc.com)‍在pom.xml中添加/修改maven打包依赖:<build><finalName>${project.artifactId}</finalName><plugins><plugin><......
  • 解决IntelliJ IDEA执行maven打包,执行java -jar命令提示jar中没有主清单属性
    问题场景IDEA执行mvncleanpackage-DskipTest=true命令或者借助工具的Maven菜单进行打包操作,然后执行java-jarapp.jar命令后,提示jar中没有主清单属性D:\WorkSpace\demo\target>java-jardemo-SNAPSHOT.jardemo-SNAPSHOT.jar中没有主清单属性原因分析这个错误通常是......
  • 【Java 基础篇】Java 比较器排序:精通自定义对象排序
    在Java编程中,排序是一个非常常见且重要的操作。Java提供了多种排序机制,其中之一就是使用比较器(Comparator)进行排序。比较器允许您自定义对象的排序方式,使您能够实现各种排序需求,从简单的对象排序到复杂的多属性排序。本篇博客将从入门到高级,详细介绍Java比较器排序的使用。什......
  • spring对象的获取及属性赋值方式
    3、spring创建第三方bean对象在Spring中,很多对象都是单实例的,在日常的开发中,我们经常需要使用某些外部的单实例对象,例如数据库连接池,下面我们来讲解下如何在spring中创建第三方bean实例。​1、导入数据库连接池的pom文件<dependency><groupId>com.alibaba</groupId><a......
  • CSS的表格属性
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</title>......
  • spring对象的获取及属性赋值方式(二)
    11、bean对象的初始化和销毁方法​在创建对象的时候,我们可以根据需要调用初始化和销毁的方法Address.javapackagecom.mashibing.bean;publicclassAddress{privateStringprovince;privateStringcity;privateStringtown;publicAddress(){Sy......