SpringMVC数据格式化

本文详细介绍了如何在SpringMVC中利用@DateTimeFormat和@NumberFormat注解进行数据格式化,包括XML配置、日期字符串转Date实例、测试步骤以及数字字符串转整数的过程。通过实例展示了解决格式不匹配导致的错误处理。
摘要由CSDN通过智能技术生成

SpringMVC数据格式化:@DateTimeFormat与@NumberFormat的使用

一、数据格式化


数据格式化是对数据进行规范化的一种处理方式,它在一定程度上规范了前端传来的数据信息,方便后台对数据进行进一步处理,SpringMVC可以通过注解形式实现数据格式化

二、步骤

1.配置xml

代码如下:

	<!-- 配置数据格式化注解所需要的bean -->
	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	</bean>

2.@DateTimeFormat:将String转换成Date

实体类(示例):

package com.lanqiao.entity;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;

public class Student {

	private int id;
	private String name;
	
	@DateTimeFormat(pattern = "yyyy-MM-dd")//格式化前台传递来的数据。将前台传的数据的格式固定为yyyy-MM-dd
	private Date birthday;
	
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(int id, String name, Date birthday) {
		super();
		this.id = id;
		this.name = name;
		this.birthday = birthday;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

其中为Date对象添加@DateTimeFormat注解

	@DateTimeFormat(pattern = "yyyy-MM-dd")//格式化前台传递来的数据。将前台传的数据的格式固定为yyyy-MM-dd
	private Date birthday;

3.测试

前端:

	<!-- 测试数据格式化 -->
	<form action="testDateTimeFormat" method="get" >
		编号:<input type="text" name="id"  ><br>
		姓名:<input type="text" name="name"><br
		出生日期:<input type="text" name="birthday"><br>
		<input type="submit" value="修改"/>
	</form>

在这里插入图片描述
Controller:

	//测试数据格式化
	@RequestMapping(value = "testDateTimeFormat")
	public String testDateTimeFormat(Student student,BindingResult result) {
		
		if(result.getErrorCount()>0) {
			for(FieldError error:result.getFieldErrors()) {
				System.out.println("error:"+error.getDefaultMessage());
			}
		}
		System.out.println(student.getId()+","+student.getName()+","+student.getBirthday());
		return "success";
	}

效果:
在这里插入图片描述
如果我们没有按照"yyyy-MM-dd"规定格式输入,则会报错
在这里插入图片描述

错误信息:

error:Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘birthday’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value ‘2021/04/04’; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2021/04/04]

4.@NumberFormat:将String转换为int

对数字格式化需要添加注解 @NumberFormat

	@NumberFormat(pattern = "#,###")
	private int id;

前端:
在这里插入图片描述

控制台:
在这里插入图片描述

可以看出:
@NumberFormat自动将String类型在规则限制下转换为int类型

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温文艾尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值