Spring自定义标签流程

分享自己的一篇学习笔记,共同进步。

首先我们要有一个实体类UserInfo进行参数接收

public class UserInfo {
    private String id;
    private String username;
    private String age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "UserInfo{" +
                "username='" + username + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

1、模仿一个继承了 AbstractSingleBeanDefinitionParser 的子类

写出自己的处理类

public class UserInfoBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

    @Override
    protected Class<?> getBeanClass(Element element) {
        return UserInfo.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {

        String id =element.getAttribute("id");
        String username =element.getAttribute("username");
        String age = element.getAttribute("age");

        if (StringUtils.hasText(id)) {
            builder.addPropertyValue("id", id);
        }
        if (StringUtils.hasText(username)) {
            builder.addPropertyValue("username", username);
        }
        if (StringUtils.hasText(age)) {
            builder.addPropertyValue("age", age);
        }
    }
}

2、模仿一个继承了NamespaceHandlerSupport的子类

写出自己的处理类

public class UserInfoNamespaceHandler extends NamespaceHandlerSupport {
    @Override
    public void init() {
        registerBeanDefinitionParser("user-info",new UserInfoBeanDefinitionParser());
    }
}

3、指定标签值,在META-INF下定义以下三个文件

3.1、handlers用于绑定2中定义的UserNamespaceHandler

http\://www.hhynl.com/schema/userInfo=com.hhynl.custom.selftag.UserInfoNamespaceHandler

这个在使用的时候,用于定位文件,网址可以写自己的

3.2、schemas用于绑定xsd文件位置

http\://www.hhynl.com/schema/userInfo.xsd=META-INF/userInfo.xsd

3.3、xsd文件用于指定标签内容

注意:name="user-info" 要与2中的registerBeanDefinitionParser的传参名字相同;

需要有id属性。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.hhynl.com/schema/userInfo"
        elementFormDefault="qualified">
    <element name="user-info">
        <complexType>
            <attribute name ="id" type = "string"/>
            <attribute name ="username" type = "string"/>
            <attribute name ="age" type = "string"/>
        </complexType>
    </element>
</schema>

4、使用

1、定位文件位置下图是以下文件间的关联部分

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值