Binding of the 07 data format

The formatting includes springmvc date format and a digital format. In this section we will explain the format springmvc.

1, environmental constraints

  • win10 64-bit operating system
  • idea2018.1.5
  • jdk-8u162-windows-x64
  • spring4.2.4

2, the premise of restraint

3, steps

  • Net.wanho.controller.FormatController.java created in the src folder, as follows:

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class FormatController{


    @RequestMapping("/format")
    @ResponseBody
    public String testFormat(User user)
    {
        return user.toString();
    }

}

class User{
    private int id;

    private String name;

    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birthday;  //提供get/set方法

    @NumberFormat(pattern="#,###,###.#")
    private Float salary;  //提供get/set方法


    public User() {
    }

    public User(int id, String name, Date birthday, Float salary) {
        this.id = id;
        this.name = name;
        this.birthday = birthday;
        this.salary = salary;
    }

    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;
    }

    public Float getSalary() {
        return salary;
    }

    public void setSalary(Float salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birthday=" + birthday +
                ", salary=" + salary +
                '}';
    }
}

Guess you like

Origin www.cnblogs.com/alichengxuyuan/p/12554613.html