書式はspringmvc日付形式とデジタル形式を含みます。このセクションでは、フォーマットspringmvcを説明します。
1、環境制約
- win10 64ビットオペレーティングシステム
- idea2018.1.5
- JDK-8u162-Windowsのx64の
- spring4.2.4
2、拘束の前提
- 完全な注釈ベースのデモspringmvc https://www.jianshu.com/p/d1a84f07c98f
3、ステップ
- 次のようにNet.wanho.controller.FormatController.javaは、srcフォルダ内に作成しました:
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 +
'}';
}
}
- スタートTomcatは、ブラウザに入力します。http:// localhostを:8080 /フォーマット
給与= 2011年11月11日= 1,234,456.4&誕生日? これらは、データspringmvcフォーマットされています。