使用AnnotationFormatterFactory<A extends Annotation>格式化数据

 代码:

testForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试AnnotationFormatterFactory	接口</title>
</head>
<body>
<h3>测试表单数据格式化</h3>
<form action="test" method="post">
     <table>
         <tr>
         	<td><label>日期类型: </label></td>
             <td><input type="text" id="birthday" name="birthday" ></td>
         </tr>
         <tr>
         	<td><label>整数类型: </label></td>
             <td><input type="text" id="total" name="total" ></td>
         </tr>
         <tr>
         	<td><label>百分数类型: </label></td>
             <td><input type="text" id="discount" name="discount" ></td>
         </tr>
         <tr>
         	<td><label>货币类型: </label></td>
             <td><input type="text" id="money" name="money" ></td>
         </tr>
         <tr>
             <td><input id="submit" type="submit" value="提交"></td>
         </tr>
     </table>
</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试AnnotationFormatterFactory</title>
</head>
<body>
<h3>测试表单数据格式化</h3>
<form:form modelAttribute="user" method="post" action="" >
<table>
	<tr>
		<td>日期类型:</td>
		<td><form:input path="birthday"/></td>
	</tr>
	<tr>
		<td>整数类型:</td>
		<td><form:input path="total"/></td>
	</tr>
	<tr>
		<td>百分数类型:</td>
		<td><form:input path="discount"/></td>
	</tr>
	<tr>
		<td>货币类型:</td>
		<td><form:input path="money"/></td>
	</tr>
</table>
</form:form>
</body>
</html>

User.java

package com.bean;

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 {
	// 日期类型
	@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 User() {
		super();
		// TODO Auto-generated constructor stub
	}
	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() {
		// TODO Auto-generated method stub
		return "User [birthday=" + birthday + ", total=" + total + ", discount=" + discount + ", money=" + money + "]";
	}
	
}

FormatterController.java

package com.control;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.bean.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class FormatterController{
	private static final Log logger = LogFactory.getLog(FormatterController.class);
	 
	@RequestMapping(value="/{formName}")
	 public String loginForm(@PathVariable String formName){
		// 动态跳转页面
		return formName;
	}
	
	 @RequestMapping(value="/test",method=RequestMethod.POST)
	 public String test(@ModelAttribute User user, Model model) {
		 logger.info(user);
		 model.addAttribute("user", user);
	     return "success";
	 }
}

截图:




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值