一、什么是注解驅動的屬性格式化?
--在bean的屬性中設置,SpringMVC處理 方法參數綁定數據、模型數據輸出時自動通過注解應用格式化的功能。
二、注解類型
1.DateTimeFormat
@DateTimeFormat注解可以對java.util.Date、java.util.calenda等時間類型的屬性進行標注。
DateTimeFormat最常用的屬性就是 pattern,類型為String,使用自定義的時間格式化字符串,如yyyy-MM-dd
2.NumberFormat
@NumberFormat可以對類似數字類型的屬性進行標注,它擁有兩個互斥的屬性。
*pattern。類型為String,使用自定義的數字格式化串,如#,###
*style。類型是NumberFormat.Style,它有幾個常用的可選值:
CURRENCY:貨幣類型
NUMBER:正常數字類型PERCENT:百分數類型
實例:
1.導入所需jar包
2.在web.xml配置前端控制器
<!-- 配置springmvc的前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.編寫一個實體類
package com.dj.pojo;
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 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;
}
}
4.編寫一個controller
package com.dj.controller;
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;
import com.dj.pojo.User;
@Controller
public class UserController {
/**
* 動態跳轉頁面
* @param pagename
* @return
*/
@RequestMapping(value="/{pagename}")
public String index(@PathVariable String pagename){
return pagename;
}
/**
* 跳轉到顯示格式化后數據的頁面
* @param user
* @param model
* @return
*/
@RequestMapping(value="/test",method=RequestMethod.POST)
public String test(@ModelAttribute User user,Model model){
model.addAttribute("user",user);
return "success";
}
}
5.編寫一個測試的表單頁面
<%@ 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>Insert title here</title>
</head>
<body>
<form action="test" method="post">
日期類型:<input type="text" id="birthday" name="birthday"/><br>
整數類型:<input type="text" id="total" name="total"/><br>
百分數類型:<input type="text" id="discount" name="discount"/><br>
貨幣類型:<input type="text" id="money" name="money"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
6.編寫一個測試顯示格式化后的數據的頁面。如果希望在視圖頁面中將模型數據以格式化的方式進行渲染,則需要使用spring的頁面標簽。
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<form:form modelAttribute="user" method="post" action="">
日期類型:<form:input path="birthday"/><br>
整數類型:<form:input path="total"/><br>
百分數類型:<form:input path="discount"/><br>
貨幣類型:<form:input path="money"/><br>
</form:form>
7.最后編寫配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- spring可以自動去掃描base-package下面的包或者子包下面的java類 如果掃描到有spring相關注解的類,則吧這個類注冊為spring的bean -->
<context:component-scan base-package="com.dj.controller" />
<!-- 默認裝配 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 視圖解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix">
<value>/</value>
</property>
<!-- 后綴 -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
8.最后進行測試




測試成功!
