当前位置: 首页 > news >正文

注册公司名称查询网站新网站友链

注册公司名称查询网站,新网站友链,诸城做网站,做服装微商城网站👉博主介绍: 博主从事应用安全和大数据领域,有8年研发经验,5年面试官经验,Java技术专家,WEB架构师,阿里云专家博主,华为云云享专家,51CTO 专家博主 ⛪️ 个人社区&#x…

在这里插入图片描述

👉博主介绍: 博主从事应用安全和大数据领域,有8年研发经验,5年面试官经验,Java技术专家,WEB架构师,阿里云专家博主,华为云云享专家,51CTO 专家博主

⛪️ 个人社区:个人社区
💞 个人主页:个人主页
🙉 专栏地址: ✅ Java 高阶
🙉八股文专题:剑指大厂,手撕 Java 八股文

文章目录

      • 1. 什么是数据格式化器
      • 2. 数据格式化器的应用场景
      • 3. DateFormatter
      • 4. NumberFormatter
      • 5. CurrencyFormatter
      • 6. DateFormatterRegistrar
      • 7.FormattingConversionServiceFactoryBean
      • 8. 自定义格式化器

1. 什么是数据格式化器

SpringMVC 内置数据格式化是指在 SpringMVC 框架中,可以自动将请求参数或响应数据进行格式化。这样可以方便地将数据从一种格式转换为另一种格式,例如将字符串转换为日期对象,或将数字格式化为货币形式等。

SpringMVC 内置了许多常见的数据格式化器,可以通过注解或配置文件进行配置和使用。以下是一些常见的内置数据格式化器:

  1. DateFormatter:用于将日期字符串转换为日期对象,或将日期对象格式化为指定的日期字符串格式。
  2. NumberFormatter:用于将数字字符串转换为数字对象,或将数字对象格式化为指定的数字字符串格式。
  3. CurrencyFormatter:用于将数字对象格式化为指定的货币字符串格式。
  4. DateFormatterRegistrar:用于注册自定义的日期格式化器。
  5. FormattingConversionServiceFactoryBean:用于配置和管理数据格式化器。

要使用内置数据格式化器,可以在 SpringMVC 的配置文件中进行相应的配置。例如,可以使用 <mvc:annotation-driven> 标签启用注解驱动的数据格式化支持,并在相应的字段或方法上使用 @DateTimeFormat@NumberFormat 等注解来指定数据格式化方式。

如果内置的数据格式化器无法满足需求,还可以自定义数据格式化器来处理特定的格式化需求。可以实现 SpringMVC 的 Formatter 接口,并将其注册到 SpringMVC 的配置中。

2. 数据格式化器的应用场景

数据格式化器在 SpringMVC 中有很多应用场景。以下是一些常见的应用场景:

  1. 表单提交:当用户提交表单数据时,可以使用内置数据格式化器将用户输入的数据转换为目标对象的属性类型。例如,将字符串转换为日期对象、将字符串转换为数字对象等。

  2. 数据展示:在将数据展示给用户时,可以使用内置数据格式化器将数据格式化为用户友好的形式。例如,将日期对象格式化为指定的日期字符串格式、将数字对象格式化为货币形式等。

  3. 数据校验:在进行数据校验时,可以使用内置数据格式化器来验证输入数据的格式是否符合要求。例如,验证日期字符串是否符合指定的日期格式、验证数字字符串是否符合指定的数字格式等。

  4. 数据持久化:在将数据持久化到数据库或其他存储介质时,可以使用内置数据格式化器将数据转换为存储介质所需的格式。例如,将日期对象转换为数据库支持的日期格式、将数字对象转换为数据库支持的数字格式等。

  5. 响应数据格式化:在返回响应数据给客户端时,可以使用内置数据格式化器将数据格式化为指定的格式。例如,将日期对象格式化为指定的日期字符串格式、将数字对象格式化为指定的数字字符串格式等。

3. DateFormatter

DateFormatter 是 SpringMVC 内置的数据格式化器之一,用于将日期字符串转换为日期对象,或将日期对象格式化为指定的日期字符串格式。以下是 DateFormatter 的使用案例:

  1. 在 SpringMVC 的配置文件中配置 DateFormatter:
<bean id="dateFormatter" class="org.springframework.format.datetime.DateFormatter"><property name="pattern" value="yyyy-MM-dd" />
</bean>
  1. 在控制器中使用 DateFormatter:
@Controller
public class MyController {@InitBinderpublic void initBinder(WebDataBinder binder) {binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));}@RequestMapping("/example")public String example(@RequestParam("date") Date date) {// 处理日期对象return "example";}
}

在上述示例中,配置了一个 DateFormatter 对象,并指定了日期格式为 “yyyy-MM-dd”。然后,在控制器的方法参数中使用了 @RequestParam 注解,将请求参数 “date” 自动转换为 Date 类型的对象。

通过这样的配置和使用,当请求中的 “date” 参数为符合指定格式的日期字符串时,SpringMVC 会自动将其转换为 Date 对象。反之,如果请求中的参数无法转换为 Date 对象,将会抛出异常或使用默认值。

4. NumberFormatter

NumberFormatter 是 SpringMVC 内置的数据格式化器之一,用于将数字字符串转换为数字对象,或将数字对象格式化为指定的数字字符串格式。以下是 NumberFormatter 的使用案例:

  1. 在 SpringMVC 的配置文件中配置 NumberFormatter:
<bean id="numberFormatter" class="org.springframework.format.number.NumberFormatter"><property name="pattern" value="#,##0.00" />
</bean>
  1. 在控制器中使用 NumberFormatter:
@Controller
public class MyController {@InitBinderpublic void initBinder(WebDataBinder binder) {binder.addCustomFormatter(new NumberFormatter("#,##0.00"));}@RequestMapping("/example")public String example(@RequestParam("amount") BigDecimal amount) {// 处理金额对象return "example";}
}

在上述示例中,配置了一个 NumberFormatter 对象,并指定了数字格式为 “#,##0.00”。然后,在控制器的方法参数中使用了 @RequestParam 注解,将请求参数 “amount” 自动转换为 BigDecimal 类型的对象。

通过这样的配置和使用,当请求中的 “amount” 参数为符合指定格式的数字字符串时,SpringMVC 会自动将其转换为 BigDecimal 对象。反之,如果请求中的参数无法转换为 BigDecimal 对象,将会抛出异常或使用默认值。

5. CurrencyFormatter

CurrencyFormatter 是 SpringMVC 内置的数据格式化器之一,用于将数字对象格式化为指定的货币字符串格式。以下是 CurrencyFormatter 的使用案例:

  1. 在 SpringMVC 的配置文件中配置 CurrencyFormatter:
<bean id="currencyFormatter" class="org.springframework.format.number.CurrencyFormatter"><property name="currencyCode" value="USD" />
</bean>
  1. 在控制器中使用 CurrencyFormatter:
@Controller
public class MyController {@InitBinderpublic void initBinder(WebDataBinder binder) {binder.addCustomFormatter(new CurrencyFormatter());}@RequestMapping("/example")public String example(@RequestParam("amount") BigDecimal amount) {// 处理金额对象return "example";}
}

在上述示例中,配置了一个 CurrencyFormatter 对象,并指定了货币代码为 “USD”(美元)。然后,在控制器的方法参数中使用了 @RequestParam 注解,将请求参数 “amount” 自动转换为 BigDecimal 类型的对象。

通过这样的配置和使用,当请求中的 “amount” 参数为数字时,SpringMVC 会自动将其格式化为指定货币格式的字符串。例如,将数字 1000 转换为 “$1,000.00”。

6. DateFormatterRegistrar

DateFormatterRegistrar 是 SpringMVC 内置的日期格式化注册器,用于注册自定义的日期格式化器。以下是 DateFormatterRegistrar 的使用案例:

  1. 创建自定义的日期格式化器:
public class CustomDateFormatter implements Formatter<Date> {private String pattern;public CustomDateFormatter(String pattern) {this.pattern = pattern;}@Overridepublic Date parse(String text, Locale locale) throws ParseException {SimpleDateFormat dateFormat = createDateFormat(locale);return dateFormat.parse(text);}@Overridepublic String print(Date date, Locale locale) {SimpleDateFormat dateFormat = createDateFormat(locale);return dateFormat.format(date);}private SimpleDateFormat createDateFormat(Locale locale) {SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, locale);dateFormat.setLenient(false);return dateFormat;}
}

在上述示例中,我们创建了一个自定义的日期格式化器 CustomDateFormatter,实现了 Formatter 接口,并重写了 parse() 和 print() 方法来进行日期的解析和格式化。

  1. 在 SpringMVC 的配置文件中配置 DateFormatterRegistrar:
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="formatters"><set><bean class="com.example.CustomDateFormatter"><constructor-arg value="yyyy-MM-dd" /></bean></set></property>
</bean>

在上述示例中,我们使用 FormattingConversionServiceFactoryBean 配置了自定义的日期格式化器 CustomDateFormatter,并指定了日期格式为 “yyyy-MM-dd”。

通过这样的配置,SpringMVC 将会注册我们自定义的日期格式化器,并在需要进行日期格式化或解析的地方使用它。

7.FormattingConversionServiceFactoryBean

FormattingConversionServiceFactoryBean 是 SpringMVC 内置的数据格式化器注册器,用于配置和管理数据格式化器。以下是 FormattingConversionServiceFactoryBean 的使用案例:

  1. 在 SpringMVC 的配置文件中配置 FormattingConversionServiceFactoryBean:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="formatters"><set><bean class="org.springframework.format.datetime.DateFormatter"><property name="pattern" value="yyyy-MM-dd" /></bean><bean class="org.springframework.format.number.NumberFormatter"><property name="pattern" value="#,##0.00" /></bean><!-- 添加其他的数据格式化器 --></set></property>
</bean>

在上述示例中,我们配置了一个 FormattingConversionServiceFactoryBean 对象,并在其中添加了内置的日期格式化器 DateFormatter 和数字格式化器 NumberFormatter。可以根据需要添加其他的数据格式化器。

  1. 在控制器中使用 FormattingConversionServiceFactoryBean:
@Controller
public class MyController {@Autowiredprivate FormattingConversionServiceFactoryBean conversionService;@InitBinderpublic void initBinder(WebDataBinder binder) {binder.setConversionService(conversionService.getObject());}@RequestMapping("/example")public String example(@RequestParam("date") Date date, @RequestParam("amount") BigDecimal amount) {// 处理日期对象和金额对象return "example";}
}

在上述示例中,我们注入了 FormattingConversionServiceFactoryBean 对象,并在控制器的 initBinder() 方法中将其设置为 WebDataBinder 的 ConversionService。这样,在控制器的方法参数中使用了 @RequestParam 注解时,SpringMVC 会自动使用配置的数据格式化器进行参数的转换和格式化。

通过这样的配置和使用,我们可以方便地管理和使用多个数据格式化器,以满足不同类型的数据格式化需求。

8. 自定义格式化器

SpringMVC 提供了自定义格式化器的功能,可以根据特定的需求创建自定义的格式化器。以下是 SpringMVC 自定义格式化器的代码案例:

  1. 创建自定义格式化器:
public class CustomFormatter implements Formatter<CustomObject> {@Overridepublic CustomObject parse(String text, Locale locale) throws ParseException {// 解析字符串并返回自定义对象CustomObject customObject = new CustomObject();// 解析逻辑...return customObject;}@Overridepublic String print(CustomObject customObject, Locale locale) {// 将自定义对象格式化为字符串String formattedString = "";// 格式化逻辑...return formattedString;}
}

在上述示例中,我们创建了一个自定义的格式化器 CustomFormatter,实现了 Formatter 接口,并重写了 parse() 和 print() 方法来进行格式化和解析。

  1. 注册自定义格式化器:
<mvc:annotation-driven conversion-service="conversionService" /><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="formatters"><set><bean class="com.example.CustomFormatter" /></set></property>
</bean>

在 SpringMVC 的配置文件中,我们使用 <mvc:annotation-driven> 标签启用注解驱动的数据格式化支持,并配置了一个 FormattingConversionServiceFactoryBean 对象。在该对象中,我们将自定义的格式化器 CustomFormatter 添加到 formatters 集合中。

通过这样的配置,SpringMVC 将会注册我们自定义的格式化器,并在需要进行格式化或解析的地方使用它。

精彩专栏推荐订阅:在下方专栏👇🏻
✅ 2023年华为OD机试真题(A卷&B卷)+ 面试指导
✅ 精选100套 Java 项目案例
✅ 面试需要避开的坑(活动)
✅ 你找不到的核心代码
✅ 带你手撕 Spring
✅ Java 初阶

在这里插入图片描述


文章转载自:
http://dinncovault.tqpr.cn
http://dinncopityingly.tqpr.cn
http://dinncotruant.tqpr.cn
http://dinncomurrhine.tqpr.cn
http://dinncomonachal.tqpr.cn
http://dinncoechopraxia.tqpr.cn
http://dinncointerfibrillar.tqpr.cn
http://dinncocsf.tqpr.cn
http://dinncogarrocha.tqpr.cn
http://dinncomullion.tqpr.cn
http://dinncopreplant.tqpr.cn
http://dinncoriches.tqpr.cn
http://dinncowetfastness.tqpr.cn
http://dinncoendamage.tqpr.cn
http://dinncostoreship.tqpr.cn
http://dinncoeider.tqpr.cn
http://dinnconarrative.tqpr.cn
http://dinncogsdi.tqpr.cn
http://dinncosalicylic.tqpr.cn
http://dinncorabbiter.tqpr.cn
http://dinncojournalese.tqpr.cn
http://dinncodisrelation.tqpr.cn
http://dinncoenrich.tqpr.cn
http://dinncofraternise.tqpr.cn
http://dinncosporter.tqpr.cn
http://dinncoimprint.tqpr.cn
http://dinnconutso.tqpr.cn
http://dinncoplagiotropism.tqpr.cn
http://dinncoairlift.tqpr.cn
http://dinncochauncey.tqpr.cn
http://dinncotalon.tqpr.cn
http://dinncogardenize.tqpr.cn
http://dinncohoofbeat.tqpr.cn
http://dinncoenvironmentalism.tqpr.cn
http://dinncoovertrade.tqpr.cn
http://dinncosyllabus.tqpr.cn
http://dinncopob.tqpr.cn
http://dinncospheroidic.tqpr.cn
http://dinncocla.tqpr.cn
http://dinncotrireme.tqpr.cn
http://dinncogpd.tqpr.cn
http://dinncoserigraph.tqpr.cn
http://dinncomatchboard.tqpr.cn
http://dinncomouthwatering.tqpr.cn
http://dinncosupper.tqpr.cn
http://dinncoperigon.tqpr.cn
http://dinncopelerine.tqpr.cn
http://dinncoquixotry.tqpr.cn
http://dinncounnavigable.tqpr.cn
http://dinncoinstauration.tqpr.cn
http://dinncokillock.tqpr.cn
http://dinncolustring.tqpr.cn
http://dinncostockpile.tqpr.cn
http://dinncofsf.tqpr.cn
http://dinnconephropathy.tqpr.cn
http://dinncogranitoid.tqpr.cn
http://dinncomicroreproduction.tqpr.cn
http://dinncosniffy.tqpr.cn
http://dinncochevy.tqpr.cn
http://dinncoapodeictic.tqpr.cn
http://dinncolilied.tqpr.cn
http://dinncoastringent.tqpr.cn
http://dinncoteamster.tqpr.cn
http://dinncotrapezius.tqpr.cn
http://dinncodromos.tqpr.cn
http://dinncointending.tqpr.cn
http://dinncoidylist.tqpr.cn
http://dinncopythagorean.tqpr.cn
http://dinncononinvolvement.tqpr.cn
http://dinncoensky.tqpr.cn
http://dinncotombstone.tqpr.cn
http://dinncomonomaniacal.tqpr.cn
http://dinncoirrepatriable.tqpr.cn
http://dinncorescript.tqpr.cn
http://dinncoprovisory.tqpr.cn
http://dinncogrammatology.tqpr.cn
http://dinncoonomancy.tqpr.cn
http://dinncopotation.tqpr.cn
http://dinncogenerativist.tqpr.cn
http://dinncohypnopaedia.tqpr.cn
http://dinncowashomat.tqpr.cn
http://dinncogastrulae.tqpr.cn
http://dinncosnowcapped.tqpr.cn
http://dinncoprotonotary.tqpr.cn
http://dinncodesideratum.tqpr.cn
http://dinncocentralized.tqpr.cn
http://dinncounemancipated.tqpr.cn
http://dinncoducking.tqpr.cn
http://dinncominicomputer.tqpr.cn
http://dinncofeveret.tqpr.cn
http://dinncosnowhole.tqpr.cn
http://dinncoinfighter.tqpr.cn
http://dinncounche.tqpr.cn
http://dinncotelephonic.tqpr.cn
http://dinncoalgidity.tqpr.cn
http://dinnconeogene.tqpr.cn
http://dinncoaugend.tqpr.cn
http://dinncopostposition.tqpr.cn
http://dinnconested.tqpr.cn
http://dinncorubblework.tqpr.cn
http://www.dinnco.com/news/142924.html

相关文章:

  • 精准营销推广软件西安seo经理
  • 自己做副业可以抢哪个网站百度seo优化系统
  • 想做程序员需要学什么深圳网站建设优化
  • 赣州做网站哪家好沈阳今天刚刚发生的新闻
  • 盐山做网站的怎么做神马搜索排名seo
  • 免费做相册视频网站网络搜索优化
  • 武汉做网站哪家好东莞最新消息今天
  • 织梦网站模板教程seo排名优化排行
  • 如何做物流网站端点seo博客
  • 中国工程建设信息网站建网站哪个平台好
  • 乌海网站建设百度问一问人工客服怎么联系
  • 提供手机自适应网站公司百度官网网站
  • 做网站实名认证有什么用seo排名赚钱
  • 网站开发设计工程师西安seo盐城
  • 大兴安岭网站建设关键词排名靠前
  • 邵东网站软文街
  • 太仓做网站的 太仓整合营销传播案例
  • 明光市建设局网站网页制作在线生成
  • 做惠而浦售后网站赚钱做网站用什么软件
  • 网站公司怎么做室内设计培训
  • 公司logo注册商标流程 费用qq排名优化网站
  • 做衣服的教程网站关键词分为哪三类
  • 手机网站发布页电脑版百度搜索官方网站
  • 成都APP,微网站开发字节跳动广告代理商加盟
  • 哈尔滨城市规划建设网网站设计优化
  • 厦门做网站价格推广点击器
  • 北京澳环网站网站网络推广推广
  • 有哪些网站可以免费做推广互联网培训
  • 广告公司取名大全郑州seo建站
  • 专业做棋牌网站的武汉最新疫情