@DateTimeFormat、@NumberFormat

用于格式转换

package com.example.demo.model;

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

public class TUser {
    private Integer userId;

    private String userName;

    private String password;

    private String phone;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date birthday;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }

    public Date getBirthday() {
        return birthday;
    }

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

JSP页面:

<form action="testFormat" method="post">
        <input type="text" name="date">
        <input type="submit" value="数据格式化">
    </form>
后台方法:

@RequestMapping("/testFormat")
    public String testFormat(Address address){
        System.out.println(address);
        return "success";
    }
实体类:

//时间
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date date;
    //浮点数
    @NumberFormat(pattern="#,###,###.#")
    private Float ind;
jsp页面输入:2017-1-1,后台的实体类可以把它转换成Date类型。

springmvc配置文件需要加入:

<mvc:annotation-driven></mvc:annotation-driven>
经过测试,发现要让注解起效,那么在mvc:annotation-driven这个标签里面就不能在加其它属性,这里只测试了自定义类型转换,发现用了自定义类型转换后,那么对数据进行格式的注解就用不了了。
--------------------- 
作者:程序搬砖员 
来源:CSDN 
原文:https://blog.csdn.net/java_xuetu/article/details/80066588 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值