读书

Spring MVC数据格式化(Formatter)

2022-06-14  本文已影响0人  Java七七

Spring MVC 框架的 Formatter<T> 与 Converter<S, T> 一样,也是一个可以将一种数据类型转换成另一种数据类型的接口。不同的是,Formatter 的源类型必须是 String 类型,而 Converter 的源类型可以是任意数据类型。Formatter 更适合 Web 层,而 Converter 可以在任意层中。所以对于需要转换表单中的用户输入的情况,应该选择 Formatter,而不是 Converter。

在 Web 应用中由 HTTP 发送的请求数据到控制器中都是以 String 类型获取,因此在 Web 应用中选择 Formatter<T> 比选择 Converter<S, T> 更加合理。

内置的格式化转换器

Spring MVC 提供了几个内置的格式化转换器,具体如下。

自定义格式化转换器

自定义格式化转换器就是编写一个实现 org.springframework.format.Formatter 接口的 Java 类。该接口声明如下。

<pre class="java sh_java snippet-formatted sh_sourceCode" shownum="false" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

</pre>

这里的 T 表示由字符串转换的目标数据类型。该接口有 parse 和 print 两个接口方法,自定义格式化转换器类必须覆盖它们。

<pre class="java sh_java snippet-formatted sh_sourceCode" shownum="false" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

</pre>

parse 方法的功能是利用指定的 Locale 将一个 String 类型转换成目标类型,print 方法与之相反,用于返回目标对象的字符串表示。

示例

下面通过具体应用讲解自定义格式化转换器的用法,本节示例基于《@Controller和@RequestMapping注解》一节中的 springmvcDemo2 程序。

1. 创建实体类

创建 net.biancheng.po 包,并在该包中创建 User 实体类,代码如下。

<pre class="java sh_java snippet-formatted sh_sourceCode" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

  1. package net.biancheng.po;

  2. import java.util.Date;

  3. public class User {

  4. private String name;

  5. private Integer age;

  6. private Double height;

  7. private Date createDate;

  8. /*省略setter和getter方法/

  9. }

</pre>

2. 创建控制器类

创建 net.biancheng.controller 包,并在该包中创建 FormatterController 控制器类,代码如下。

<pre class="java sh_java snippet-formatted sh_sourceCode" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

  1. package net.biancheng.controller;

  2. import org.springframework.stereotype.Controller;

  3. import org.springframework.ui.Model;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import net.biancheng.po.User;

  6. @Controller

  7. public class FormatterController {

  8. @RequestMapping("/formatter")

  9. public String myFormatter(User us, Model model) {

  10. model.addAttribute("user", us);

  11. return "showUser";

  12. }

  13. }

</pre>

3. 创建自定义格式化转换器类

创建 net.biancheng.formatter 包,并在该包中创建 MyFormatter 的自定义格式化转换器类,代码如下。

<pre class="java sh_java snippet-formatted sh_sourceCode" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

  1. package net.biancheng.formatter;

  2. import java.text.ParseException;

  3. import java.text.SimpleDateFormat;

  4. import java.util.Date;

  5. import java.util.Locale;

  6. import org.springframework.format.Formatter;

  7. import org.springframework.stereotype.Component;

  8. @Component

  9. public class MyFormatter implements Formatter<Date> {

  10. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

  11. public String print(Date object, Locale arg1) {

  12. return dateFormat.format(object);

  13. }

  14. public Date parse(String source, Locale arg1) throws ParseException {

  15. return dateFormat.parse(source); // Formatter只能对字符串转换

  16. }

  17. }

</pre>

4. 注册格式化转换器

在 springmvc-servlet.xml 配置文件中注册格式化转换器,具体代码如下:

<pre class="xml sh_xml snippet-formatted sh_sourceCode" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

  1. <bean id="conversionService"

  2. class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

  3. <property name="formatters">

  4. <set>

  5. <bean class="net.biancheng.formatter.MyFormatter" />

  6. </set>

  7. </property>

  8. </bean>

  9. <mvc:annotation-driven conversion-service="conversionService" />

</pre>

5. 创建相关视图

创建添加用户页面 addUser.jsp,代码如下。

<pre class="xml sh_xml snippet-formatted sh_sourceCode" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>添加用户</title>
  8. </head>
  9. <body>
  10. <form action="${pageContext.request.contextPath}/formatter" method="post">
  11. 用户名:<input type="text" name="name" />

  12. 年龄:<input type="text" name="age" />

  13. 身高:<input type="text" name="height" />

  14. 创建日期:<input type="text" name="createDate" />

  15. <input type="submit" value="提交" />
  16. </form>
  17. </body>
  18. </html>

</pre>

创建信息显示页面 showUser.jsp,代码如下。

<pre class="xml sh_xml snippet-formatted sh_sourceCode" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  4. <html>

  5. <head>

  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  7. <title>用户信息</title>

  8. </head>

  9. <body>

  10. 您创建的用户信息如下:

  11. <br />

  12. 用户名:${user.name }

  13. <br />

  14. 年龄:${user.age }

  15. <br />

  16. 身高:${user.height }

  17. <br />

  18. 创建日期:${user.createDate }

  19. </body>

  20. </html>

</pre>

6. 测试运行

访问地址:http://localhost:8080/springmvcDemo2/addUser

image.png
image.png
上一篇 下一篇

猜你喜欢

热点阅读