<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Spring Boot – 自定義`PropertyEditor`配置示例 > 原文: [https://howtodoinjava.com/spring-boot/custom-property-editor-example/](https://howtodoinjava.com/spring-boot/custom-property-editor-example/) `PropertyEditor`最初是[ JavaBeans 規范](https://docs.oracle.com/javase/7/docs/api/java/beans/PropertyEditor.html)的一部分。 Spring 還大量使用`PropertyEditor`來以與對象本身不同的方式表示屬性,例如從 http 請求參數解析人類可讀的輸入,或在視圖層中顯示純 Java 對象的人類可讀的值。 Spring 在`org.springframework.beans.propertyeditors`包中有許多內置的`PropertyEditor`,例如用于`Boolean`,`Currency`和`URL`。 這些編輯器中的某些默認情況下已注冊,而某些則在需要時需要注冊。 您還可以**創建自定義的`PropertyEditor`**,以防萬一 – 默認屬性編輯器無用。 假設我們正在創建一個用于圖書管理的應用程序。 現在,人們也可以通過 [ISBN](https://en.wikipedia.org/wiki/International_Standard_Book_Number) 搜索圖書。 另外,您將需要在網頁中顯示 ISBN 詳細信息。 ## 創建自定義`PropertyEditor` 要創建自定義屬性編輯器,您將需要擴展`java.beans.PropertyEditorSupport`類。 #### `IsbnEditor.java` ```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` ```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` ```java @Controller public class HomeController { //... @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Isbn.class, new IsbnEditor()); } } ``` ## 使用自定義屬性編輯器接受輸入并顯示值 現在,創建并注冊自定義屬性編輯器后,就可以使用它。 您可以在控制器中使用它來接受輸入,如下所示: #### `HomeController.java` ```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`非常簡單,但是您可以在那里擁有完整的規則和驗證,并且可以使用。 要顯示提供的值,不需要特殊的方法。 只是普通的舊 Spring 方式。 #### `index.jsp` ```java <!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` ```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` 驗證服務器中的日志,并在瀏覽器中顯示為 isbn 作為請求輸入正確接收的路徑參數。 ```java 2017-03-16 13:40:00 - You searched for book with ISBN :: 978-3-16-148410-0 ``` ![Spring Custom Property Editor Example](https://img.kancloud.cn/68/0b/680b4222ca0e28228a71eb1430ae1803_502x141.jpg) Spring 自定義屬性編輯器示例 將我的問題放在評論部分。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看