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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM