原文: https://howtodoinjava.com/spring-boot/custom-property-editor-example/
PropertyEditor最初是 JavaBeans 规范的一部分。 Spring 还大量使用PropertyEditor来以与对象本身不同的方式表示属性,例如从 http 请求参数解析人类可读的输入,或在视图层中显示纯 Java 对象的人类可读的值。
Spring 在org.springframework.beans.propertyeditors包中有许多内置的PropertyEditor,例如用于Boolean,Currency和URL。 这些编辑器中的某些默认情况下已注册,而某些则在需要时需要注册。
您还可以创建自定义的PropertyEditor,以防万一 – 默认属性编辑器无用。 假设我们正在创建一个用于图书管理的应用程序。 现在,人们也可以通过 ISBN 搜索图书。 另外,您将需要在网页中显示 ISBN 详细信息。
创建自定义PropertyEditor
要创建自定义属性编辑器,您将需要扩展java.beans.PropertyEditorSupport类。
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 {@Overridepublic void setAsText(String text) throws IllegalArgumentException {if (StringUtils.hasText(text)) {setValue(new Isbn(text.trim()));} else {setValue(null);}}@Overridepublic 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 应用程序中注册自定义属性编辑器。 要注册,您将需要创建一个带有注解的方法 – @InitBinder。 在应用程序启动时,将扫描此注解,并且所有检测到的方法均应具有接受WebDataBinder作为参数的签名。
永远记住,PropertyEditor不是线程安全的。 您应该始终为每个Web请求创建一个新的自定义编辑器实例,并将其注册到WebDataBinder。
HomeController.java
@Controllerpublic class HomeController {//...@InitBinderpublic void initBinder(WebDataBinder binder) {binder.registerCustomEditor(Isbn.class, new IsbnEditor());}}
使用自定义属性编辑器接受输入并显示值
现在,创建并注册自定义属性编辑器后,就可以使用它。 您可以在控制器中使用它来接受输入,如下所示:
HomeController.java
@Controllerpublic 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";}@InitBinderpublic void initBinder(WebDataBinder binder) {binder.registerCustomEditor(Isbn.class, new IsbnEditor());}}
只是看看我们如何不直接在@PathVariable Isbn isbn变量中接受 ISBN 值。 我们的IsbnEditor非常简单,但是您可以在那里拥有完整的规则和验证,并且可以使用。
要显示提供的值,不需要特殊的方法。 只是普通的旧 Spring 方式。
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>
示例
现在,通过运行 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;@SpringBootApplicationpublic 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
验证服务器中的日志,并在浏览器中显示为 isbn 作为请求输入正确接收的路径参数。
2017-03-16 13:40:00 - You searched for book with ISBN :: 978-3-16-148410-0

Spring 自定义属性编辑器示例
将我的问题放在评论部分。
学习愉快!
