• Spring Boot – 自定义PropertyEditor


    前言

    PropertyEditor最初是属于Java Bean规范定义的,有意思的是,Spring也大规模的使用了PropertyEditors,以便实现以各种形式展现对象的属性;

    举个例子,常见的用于解析Http请求参数,通常需要在展现层把原始Java对象解析成对人友好的参数,这时候就经常需要用到自定义PropertyEditor

    org.springframework.beans.propertyeditors 包下,Spring已经内置了一些PropertyEditors,如解析Boolean, Currency, 和URL对象;然而这只是其中一部分常见的editors,在真实项目开发过程中,往往不满足我们的业务需求;

    当默认的这些PropertyEditors不满足我们的需求的时候,我们需要自定义PropertyEditor,举个例子,假如我们要开发一个图书管理的应用,实现可以通过 ISBN去搜索图书,同样,图书详情里也需要展示ISBN信息,这里我们可以通过自定义PropertyEditor实现,详细开发过程如下:

    创建自定义PropertyEditor

    我们需要继承java.beans.PropertyEditorSupport 类来实现自定义PropertyEditor,如下所示:
    IsbnEditor.java

    package com.howtodoinjava.app.editors;
     
    import java.beans.PropertyEditorSupport;
    import org.springframework.util.StringUtils;
    import com.howtodoinjava.app.model.Isbn;
     
    public class IsbnEditor extends PropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (StringUtils.hasText(text)) {
                setValue(new Isbn(text.trim()));
            } else {
                setValue(null);
            }
        }
     
        @Override
        public String getAsText() {
            Isbn isbn = (Isbn) getValue();
            if (isbn != null) {
                return isbn.getIsbn();
            } else {
                return "";
            }
        }
    }
    

    其中Isbn 类如下:
    Isbn.java

    package com.howtodoinjava.app.model;
     
    public class Isbn {
        private String isbn;
     
        public Isbn(String isbn) {
            this.isbn = isbn;
        }
     
        public String getIsbn() {
            return isbn;
        }
         
        public String getDisplayValue() {
            return isbn;
        }
    }
    

    注册自定义PropertyEditor

    下一步需要在Spring应用中注册我们刚刚编写的自定义PropertyEditor

    注册很简单,只需要创建一个带@InitBinder注解的方法,其中该方法需要接收一个WebDataBinder类型的参数;

    注意事项:
    PropertyEditors并不是线程安全的,对于每一个请求,我们都需要new一个PropertyEditor对象,并用WebDataBinder去注册;

    HomeController.java

    @Controller
    public class HomeController {
     
        //...
     
        @InitBinder
        public void initBinder(WebDataBinder binder) {
          binder.registerCustomEditor(Isbn.class, new IsbnEditor());
        }
    }
    

    通过自定义PropertyEditor接收参数并展现

    当我们创建完自定义PropertyEditor并注册后,就可以在Controller里使用它了,
    HomeController.java

    @Controller
    public class HomeController {
         
        private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
     
        @RequestMapping(value = "/books/{isbn}", method = RequestMethod.GET)
        public String getBook(@PathVariable Isbn isbn, Map<String, Object> model)
        {
            LOGGER.info("You searched for book with ISBN :: " + isbn.getIsbn());
            model.put("isbn", isbn);
            return "index";
        }
         
        @InitBinder
        public void initBinder(WebDataBinder binder) {
          binder.registerCustomEditor(Isbn.class, new IsbnEditor());
        }
    }
    

    现在我们就可以直接通过@PathVariable Isbn isbn去接收isbn参数了,目前我们的IsbnEditor非常简单,但是我们可以在这个基础上添加很多校验规则,非常简便;
    接下来,我们可以编写一个jsp文件,去展示信息:
    index.jsp

    <!DOCTYPE html>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html lang="en">
    <body>
        <h2>ISBN You searched is :: ${ isbn.displayValue }</h2>
    </body>
    </html>
    

    Demo测试

    直接运行Spring Boot应用即可;
    SpringBootWebApplication.java

    package com.howtodoinjava.app.controller;
     
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
     
    @SpringBootApplication
    public class SpringBootWebApplication extends SpringBootServletInitializer {
         
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SpringBootWebApplication.class, args);
        }
    }
    

    在浏览器中输入http://localhost:8080/books/978-3-16-148410-0 地址测试;
    观察后台日志打印及前端展现:

    2017-03-16 13:40:00 - You searched for book with ISBN :: 978-3-16-148410-0
    

    file

  • 相关阅读:
    1-1圆柱体的表面积
    vue的重定向和404
    新特性
    vue的一些概念
    关于ES6包的导出和导入
    vue-父组件和子组件的交互
    vue-组件
    vue---指令
    angular基础--指令2
    angular基础
  • 原文地址:https://www.cnblogs.com/chenpi/p/9785946.html
Copyright © 2020-2023  润新知