网站建设要学哪些软件,中国企业500强2022,wordpress 市场占有率,石家庄网站建设销售电话#x1f449;博主介绍#xff1a; 博主从事应用安全和大数据领域#xff0c;有8年研发经验#xff0c;5年面试官经验#xff0c;Java技术专家#xff0c;WEB架构师#xff0c;阿里云专家博主#xff0c;华为云云享专家#xff0c;51CTO 专家博主 ⛪️ 个人社区#x… 博主介绍 博主从事应用安全和大数据领域有8年研发经验5年面试官经验Java技术专家WEB架构师阿里云专家博主华为云云享专家51CTO 专家博主 ⛪️ 个人社区个人社区 个人主页个人主页 专栏地址 ✅ Java 高阶 八股文专题剑指大厂手撕 Java 八股文 文章目录 1. 什么是数据格式化器2. 数据格式化器的应用场景3. DateFormatter4. NumberFormatter5. CurrencyFormatter6. DateFormatterRegistrar7.FormattingConversionServiceFactoryBean8. 自定义格式化器 1. 什么是数据格式化器
SpringMVC 内置数据格式化是指在 SpringMVC 框架中可以自动将请求参数或响应数据进行格式化。这样可以方便地将数据从一种格式转换为另一种格式例如将字符串转换为日期对象或将数字格式化为货币形式等。
SpringMVC 内置了许多常见的数据格式化器可以通过注解或配置文件进行配置和使用。以下是一些常见的内置数据格式化器
DateFormatter用于将日期字符串转换为日期对象或将日期对象格式化为指定的日期字符串格式。NumberFormatter用于将数字字符串转换为数字对象或将数字对象格式化为指定的数字字符串格式。CurrencyFormatter用于将数字对象格式化为指定的货币字符串格式。DateFormatterRegistrar用于注册自定义的日期格式化器。FormattingConversionServiceFactoryBean用于配置和管理数据格式化器。
要使用内置数据格式化器可以在 SpringMVC 的配置文件中进行相应的配置。例如可以使用 mvc:annotation-driven 标签启用注解驱动的数据格式化支持并在相应的字段或方法上使用 DateTimeFormat 、 NumberFormat 等注解来指定数据格式化方式。
如果内置的数据格式化器无法满足需求还可以自定义数据格式化器来处理特定的格式化需求。可以实现 SpringMVC 的 Formatter 接口并将其注册到 SpringMVC 的配置中。
2. 数据格式化器的应用场景
数据格式化器在 SpringMVC 中有很多应用场景。以下是一些常见的应用场景 表单提交当用户提交表单数据时可以使用内置数据格式化器将用户输入的数据转换为目标对象的属性类型。例如将字符串转换为日期对象、将字符串转换为数字对象等。 数据展示在将数据展示给用户时可以使用内置数据格式化器将数据格式化为用户友好的形式。例如将日期对象格式化为指定的日期字符串格式、将数字对象格式化为货币形式等。 数据校验在进行数据校验时可以使用内置数据格式化器来验证输入数据的格式是否符合要求。例如验证日期字符串是否符合指定的日期格式、验证数字字符串是否符合指定的数字格式等。 数据持久化在将数据持久化到数据库或其他存储介质时可以使用内置数据格式化器将数据转换为存储介质所需的格式。例如将日期对象转换为数据库支持的日期格式、将数字对象转换为数据库支持的数字格式等。 响应数据格式化在返回响应数据给客户端时可以使用内置数据格式化器将数据格式化为指定的格式。例如将日期对象格式化为指定的日期字符串格式、将数字对象格式化为指定的数字字符串格式等。
3. DateFormatter
DateFormatter 是 SpringMVC 内置的数据格式化器之一用于将日期字符串转换为日期对象或将日期对象格式化为指定的日期字符串格式。以下是 DateFormatter 的使用案例
在 SpringMVC 的配置文件中配置 DateFormatter
bean iddateFormatter classorg.springframework.format.datetime.DateFormatterproperty namepattern valueyyyy-MM-dd /
/bean在控制器中使用 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 的使用案例
在 SpringMVC 的配置文件中配置 NumberFormatter
bean idnumberFormatter classorg.springframework.format.number.NumberFormatterproperty namepattern value#,##0.00 /
/bean在控制器中使用 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 的使用案例
在 SpringMVC 的配置文件中配置 CurrencyFormatter
bean idcurrencyFormatter classorg.springframework.format.number.CurrencyFormatterproperty namecurrencyCode valueUSD /
/bean在控制器中使用 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 的使用案例
创建自定义的日期格式化器
public class CustomDateFormatter implements FormatterDate {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() 方法来进行日期的解析和格式化。
在 SpringMVC 的配置文件中配置 DateFormatterRegistrar
bean classorg.springframework.format.support.FormattingConversionServiceFactoryBeanproperty nameformatterssetbean classcom.example.CustomDateFormatterconstructor-arg valueyyyy-MM-dd //bean/set/property
/bean在上述示例中我们使用 FormattingConversionServiceFactoryBean 配置了自定义的日期格式化器 CustomDateFormatter并指定了日期格式为 “yyyy-MM-dd”。
通过这样的配置SpringMVC 将会注册我们自定义的日期格式化器并在需要进行日期格式化或解析的地方使用它。
7.FormattingConversionServiceFactoryBean
FormattingConversionServiceFactoryBean 是 SpringMVC 内置的数据格式化器注册器用于配置和管理数据格式化器。以下是 FormattingConversionServiceFactoryBean 的使用案例
在 SpringMVC 的配置文件中配置 FormattingConversionServiceFactoryBean
bean idconversionService classorg.springframework.format.support.FormattingConversionServiceFactoryBeanproperty nameformatterssetbean classorg.springframework.format.datetime.DateFormatterproperty namepattern valueyyyy-MM-dd //beanbean classorg.springframework.format.number.NumberFormatterproperty namepattern value#,##0.00 //bean!-- 添加其他的数据格式化器 --/set/property
/bean在上述示例中我们配置了一个 FormattingConversionServiceFactoryBean 对象并在其中添加了内置的日期格式化器 DateFormatter 和数字格式化器 NumberFormatter。可以根据需要添加其他的数据格式化器。
在控制器中使用 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 自定义格式化器的代码案例
创建自定义格式化器
public class CustomFormatter implements FormatterCustomObject {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() 方法来进行格式化和解析。
注册自定义格式化器
mvc:annotation-driven conversion-serviceconversionService /bean idconversionService classorg.springframework.format.support.FormattingConversionServiceFactoryBeanproperty nameformatterssetbean classcom.example.CustomFormatter //set/property
/bean在 SpringMVC 的配置文件中我们使用 mvc:annotation-driven 标签启用注解驱动的数据格式化支持并配置了一个 FormattingConversionServiceFactoryBean 对象。在该对象中我们将自定义的格式化器 CustomFormatter 添加到 formatters 集合中。
通过这样的配置SpringMVC 将会注册我们自定义的格式化器并在需要进行格式化或解析的地方使用它。 精彩专栏推荐订阅在下方专栏 ✅ 2023年华为OD机试真题A卷B卷 面试指导 ✅ 精选100套 Java 项目案例 ✅ 面试需要避开的坑活动 ✅ 你找不到的核心代码 ✅ 带你手撕 Spring ✅ Java 初阶 文章转载自: http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.ghkgl.cn.gov.cn.ghkgl.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.wtwhj.cn.gov.cn.wtwhj.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.qgmbx.cn.gov.cn.qgmbx.cn http://www.morning.ztfzm.cn.gov.cn.ztfzm.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.lwhsp.cn.gov.cn.lwhsp.cn http://www.morning.hyjpl.cn.gov.cn.hyjpl.cn http://www.morning.ryglh.cn.gov.cn.ryglh.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.kgcss.cn.gov.cn.kgcss.cn http://www.morning.mjxgs.cn.gov.cn.mjxgs.cn http://www.morning.frsbf.cn.gov.cn.frsbf.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.aswev.com.gov.cn.aswev.com http://www.morning.ktnmg.cn.gov.cn.ktnmg.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.rtkz.cn.gov.cn.rtkz.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.fddfn.cn.gov.cn.fddfn.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.ymwcs.cn.gov.cn.ymwcs.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.jtmrx.cn.gov.cn.jtmrx.cn http://www.morning.rstrc.cn.gov.cn.rstrc.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.hbkkc.cn.gov.cn.hbkkc.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.cljmx.cn.gov.cn.cljmx.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.ghjln.cn.gov.cn.ghjln.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.hmnhp.cn.gov.cn.hmnhp.cn http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.fwkpp.cn.gov.cn.fwkpp.cn http://www.morning.cqwb25.cn.gov.cn.cqwb25.cn http://www.morning.nmfxs.cn.gov.cn.nmfxs.cn http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.uytae.cn.gov.cn.uytae.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.xhgcr.cn.gov.cn.xhgcr.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn