Spring MVC 使用AnnotationFormatterFactory格式化数据

示例【Spring MVC 使用AnnotationFormatterFactory格式化数据】

创建index.jsp


	

测试表单数据格式化

日期类型:
整数类型:
百分数类型:
货币类型:

创建User

package com.po;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
public class User {
	//日期类型
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birthday;
	//正常数字类型
	@NumberFormat(style=Style.NUMBER,pattern="#,###")
	private int total;
	//百分数类型
	@NumberFormat(style=Style.PERCENT)
	private double discount;
	//货币类型
	@NumberFormat(style=Style.CURRENCY)
	private double money;
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public double getDiscount() {
		return discount;
	}
	public void setDiscount(double discount) {
		this.discount = discount;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "User [birthday=" + birthday + ", total=" + total + ", discount=" + discount + ", money=" + money + "]";
	}
}

创建UserController

package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.po.User;
@Controller
public class UserController {
	@RequestMapping("/register")
	public String register(@ModelAttribute User user,Model model) {
		System.out.println(user);
		model.addAttribute("user", user);
		return "success";
	}
}

配置springmvc-config.xml







	
	

创建success.jsp


	
	
日期类型:
整数类型:
百分数类型:
货币类型:

启动Tomcat并访问index.jsp

Spring MVC 使用AnnotationFormatterFactory格式化数据_第1张图片Spring MVC 使用AnnotationFormatterFactory格式化数据_第2张图片

Spring MVC 使用AnnotationFormatterFactory格式化数据_第3张图片

你可能感兴趣的:(spring,mvc)