我们为什么要对数据进行处理?

需求:在上个crud中我们如果需要每次修改的时候都要把时间也记录下来

解决:在jsp中新增一个input,在employee中新增一个Data字段

问题:input输出来的数据是String,而在employee中的字段是Data数据

这个时候就需要:

  • 数据转换
  • 数据格式化
  • 数据校验

数据转换

数据校验

自定义类型处理器

jsp中发送数据

<form action="/employee" method="post">
<%--输入一个这样的数据last-name-email-gender-department-department.id:
我们就能获取到实例化的employee--%>
Employee:<input type="text" name="employee">
<input type="submit" value="提交">
</form>

control中接收数据

@RequestMapping(value = "/employee",method = RequestMethod.POST)
public String add(@RequestParam("employee") Employee employee){
System.out.println(employee);
return "redirect:/input";
}

可以知道中间需要一个String转换为employee的转换器

/*
* 1、需要添加注解组件
* 2、需要在springmvc配置文件中配置
* */
@Component
public class EmployeeConverter implements Converter<String, Employee> {
@Override
public Employee convert(String source) {
//last-name-email-gender-department-department.id
if(source!=null){
String[] values = source.split("-");
if (values!=null&&values.length == 4){
String lastName = values[0];
String email = values[1];
Integer gender = Integer.parseInt(values[2]);
Department department = new Department();
department.setDepartmentId(Integer.parseInt(values[3]));
Employee employee = new Employee(null, lastName, email, gender, department);
System.out.println(source+"--> converter-->"+employee);
return employee;
}
}
return null;
}
}

这个转换器需要配置到springmvc配置文件的注解驱动里面

<!--此处注入conversion-service属性-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/> <!--配置conversionService:
把这个配置之后还需要把这个注入到注解驱动的一个属性-->
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<!--把自定义的转换器注入到converter属性中来-->
<property name="converters" ref="employeeConverter"/>
</bean>

mvc:annotation-driven学习

会自动注册:

RequestMappingHandlerMapping 、

RequestMappingHandlerAdapter

ExceptionHandlerExceptionResolver 三个bean。

还可以

  • 支持使用 ConversionService 实例对表单参数进行类型转换
  • 支持使用 @NumberFormat@DateTimeFormat 注解完成数据类型的格式化
  • 支持使用 @Valid 注解对 JavaBean 实例进行 JSR 303 验证
  • 支持使用 @RequestBody@ResponseBody 注解

@DateTimeFormat使用

在spring-mvc配置文件配置了mvc:annotation-driven之后

在对应的pojo中

//直接就实现了string到Date
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthDate;

@NumberFormat使用

同上

@NumberFormat(pattern = "#,###,###.#")
private Float salary;

@InitBinder实现手动映射数据

由 @InitBinder 标识的方法,可以对 WebDataBinder 对 象进行初始化。WebDataBinder 是 DataBinder 的子类,用于完成由表单字段到 JavaBean 属性的绑定

@InitBinder方法不能有返回值,它必须声明为void。

@InitBinder方法的参数通常是是 WebDataBinde

比如下面这个方法就可以使映射过来的lastName数据被忽略

@InitBinder
public void initBinder(WebDataBinder dataBinder){
dataBinder.setDisallowedFields("name");
}

数据格式化

数据校验

1、如何校验?

JSR表示Java Specification Requests,Java 规范提案。JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation。用于验证bean属性注入值类型,格式等有效性。

添加依赖

<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.1.Final</version>
</dependency>

constraint

@Null 被注释的元素必须为null
@NotNull 被注释的元素不能为null
@AssertTrue 被注释的元素必须为true
@AssertFalse 被注释的元素必须为false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max,min) 被注释的元素的大小必须在指定的范围内。
@Digits(integer,fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(value) 被注释的元素必须符合指定的正则表达
@Email 被注释的元素必须是电子邮件地址
@NotEmpty 被注释的字符串必须非空

Hibernate Validator 附加的 constraint

@Length 被注释的字符串的大小必须在指定的范围内
@Range 被注释的元素必须在合适的范围内

注意点:

  • 数值检查建议使用在Stirng,Integer类型,不要使用在int类型上,因为表单值为“”时无法转换为int,但可以转换为Stirng为”“,Integer为null 。
使用@Valid

用于修饰@Component组件类,结合上面的属性级别constraint注解,给类属性字段加一层数据校验。一般搭配@ConfigurationProperties类级别注解使用,校验外部配置值的有效性。

  • @Validated所在包路径:org.springframework.validation.annotation.Validated

2、页面出错转到那个页面?

3、错误消息如何显示?如何国际化?

最新文章

  1. PCL常见编程问题
  2. LeetCode(三)
  3. redmin3 忘记管理密码找回方法
  4. (XAML)&quot;XXXX&quot; does not exist in the namespace &quot;clr-
  5. Redbean:入门(二) - Find
  6. python抓取伯乐在线的全部文章,对标题分词后存入mongodb中
  7. SGU 299.Triangle
  8. path类和directory类对文件的路径或目录进行操作
  9. xml类型转换列表显示 SQL查询
  10. P1013
  11. Caddy服务器搭建和实现文件共享
  12. HYPER -V 独立安装的 2016版本 中文版 下载好慢啊
  13. kettle并行运行时出现「Unknown error in KarafBlueprintWatcher」
  14. PHP安装Eclipse与使用
  15. VirtualBox安装Ubuntu16.04过程
  16. Hadoop环境搭建--Docker完全分布式部署Hadoop环境(菜鸟采坑吐血整理)
  17. Kaggle比赛NCFM图像分类任务简介
  18. Linux终端执行shell脚本,提示权限不够的解决办法
  19. (精)字符串,map -&gt; json对象-&gt;map(初学者必读)
  20. CSS样式--移动划过超链接鼠标变手型详解

热门文章

  1. 开源电路分享のFalling Star Board
  2. Hash源码注释解析
  3. VNC 相关
  4. 【Android编程】Java利用apktool编写Metasploit恶意后门注入工具
  5. 【NX二次开发】隐藏、显示对象UF_OBJ_set_blank_status
  6. GoLang:通过url将值从view层(.tpl)传递到controller层
  7. 《CNN Image Retrieval in PyTorch: Training and evaluati-ng CNNs for Image Retrieval in PyTorch》代码思路解读
  8. Golang学习(用代码来学习) - 第五篇
  9. ANDROID开发之GC_CONCURRENT freed
  10. MySQL 中存储时间的最佳实践