资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在SpringBoot中定制PropertyEditors方法-创新互联

这篇文章给大家介绍怎么在Spring Boot中定制PropertyEditors方法,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

十余年的娄烦网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整娄烦建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“娄烦网站设计”,“娄烦网站推广”以来,每个客户项目都认真落实执行。

Isbn类:

package com.test.bookpub.utils;

public class Isbn {
  private String isbn;

  public Isbn(String isbn) {
    this.isbn = isbn;
  }
  public String getIsbn() {
    return isbn;
  }
}

IsbnEditor类,继承PropertyEditorSupport类,setAsText完成字符串到具体对象类型的转换,getAsText完成具体对象类型到字符串的转换。

package com.test.bookpub.utils;
import org.springframework.util.StringUtils;
import java.beans.PropertyEditorSupport;

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 "";
    }
  }
}

在BookController中增加initBinder函数,通过@InitBinder注解修饰,则可以针对每个web请求创建一个editor实例。

@InitBinderpublic 
void initBinder(WebDataBinder binder) {
  binder.registerCustomEditor(Isbn.class, new IsbnEditor());
}

修改BookController中对应的函数

@RequestMapping(value = "/{isbn}", method = RequestMethod.GET)
public Map getBook(@PathVariable Isbn isbn) {
  Book book = bookRepository.findBookByIsbn(isbn.getIsbn());
  Map response = new LinkedHashMap<>();
  response.put("message", "get book with isbn(" + isbn.getIsbn() +")");
  response.put("book", book);  return response;
}

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

关于怎么在Spring Boot中定制PropertyEditors方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:怎么在SpringBoot中定制PropertyEditors方法-创新互联
地址分享:http://cdkjz.cn/article/dspojo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220