基于springboot的自定义converter例子

对于converter的理解、原理转载于:https://blog.csdn.net/cold___play/article/details/102839502?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162460403216780357268637%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=162460403216780357268637&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-3-102839502.pc_search_result_before_js&utm_term=converter&spm=1018.2226.3001.4187

例子:

导入依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

person和pet存在级联关系的javabean

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
public class Person {
    private String name;
    private Integer age;
    private Date birth;
    private Pet pet;
}
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Pet implements Serializable {
    private String name;
    private Integer age;

}

html页面,使用thymeleaf模板引擎

    <form method="post" th:action="@{/savePerson}">
        姓名:<input type="text" name="userName" value="zhangsan"><br>
        年龄:<input type="text" name="age" value="18"><br>
        生日:<input type="text" name="birth" value="2019/12/10"><br>
<!--        宠物姓名:<input type="text" name="pet.name" value="阿猫"><br>-->
<!--        宠物年龄:<input type="text" name="pet.age" value="5"><br>-->
        宠物:<input type="text" name="pet" value="阿猫,3"><br>
        <button type="submit" >保存</button>
    </form>

配置类:WebMvcConfigurer

@Configuration(proxyBeanMethods = false)//放的组件没有依赖 用false快速
public class WebConfig /*implements WebMvcConfigurer*/ {
    /*
    第②种:开启矩阵变量功能
    WebMvcConfigurer定制化SpringMVC的功能
     */
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
       return new WebMvcConfigurer() {
           @Override
           public void addFormatters(FormatterRegistry registry) {
               registry.addConverter(new Converter<String, Pet>() {

                   @Override
                   public Pet convert(String source) {
                       //阿猫,3
                       if(!StringUtils.isEmpty(source)){
                           Pet pet = new Pet();
                           String[] split = source.split(",");
                           pet.setName(split[0]);
                           pet.setAge(Integer.parseInt(split[1]));
                            return pet;
                       }
                       return null;
                   }
               });
           }
       };
    }
}

controller层

    @ResponseBody
    @PostMapping("/savePerson")
    public Person savePerson(Person person){
        return person;
    }

测试:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值