Spring之数据格式化

6 篇文章 0 订阅
2 篇文章 0 订阅

创建实体类

在每一个字段上面添加对应格式化的注解

package com.alin.AnnotationFormatter;
import java.io.Serializable;
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 implements Serializable{

	private static final long serialVersionUID = 1L;

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

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
}

创建控制器

package com.alin.AnnotationFormatter;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FormatterController {

	@RequestMapping(value="/Form")
	public String registerForm(){
		
		return "Formatter/Form";
	}
	
	@RequestMapping(value = "/formatter")
	public String formatter(@ModelAttribute User user, Model model) {
		model.addAttribute("user",user);
		return "Formatter/success";
	}
}

Form.jsp 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>格式化</title>
  </head>
  
  <body>
    <form:form  method="post" action="formatter">
		<span>日期类型</span>
		<input type="text" id="birthday" name="birthday"/><br/>
		<span>整数类型</span>
		<input type="text" id="total" name="total"/><br/>
		<span>百分数类型</span>
		<input type="text" id="discount" name="discount"/><br/>
		<span>货币类型</span>
		<input type="text" id="money" name="money"/>
		<input id="submit" type="submit" value="提交"/>
	</form:form>
  </body>
</html>

 success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>格式后数据</title>
</head>
<body>
	<form:form modelAttribute="user" method="post" action="">
		<span>日期类型</span>
		<form:input path="birthday" /><br/>
		<span>整数类型</span>
		<form:input path="total" /><br/>
		<span>百分数类型</span>
		<form:input path="discount" /><br/>
		<span>货币类型</span>
		<form:input path="money" />
	</form:form>
</body>
</html>

运行效果

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值