特殊數(shù)據(jù)處理
需要結(jié)合封裝類
數(shù)據(jù)格式化
時(shí)間:
org.springframework.format.annotation.DateTimeFormat
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;
//轉(zhuǎn)換后可以獲取頁(yè)面?zhèn)鬟f的字符串時(shí)間(從頁(yè)面到方法)
//從方法到頁(yè)面:頁(yè)面是不能直接獲取date類型,頁(yè)面需要對(duì)date類型做格式化
精度:
org.springframework.format.annotation.NumberFormat
@NumberFormat(pattern="#,###,###.#")
private Double salary;//正常接受價(jià)格精度。
用字符串可以解決這些麻煩的轉(zhuǎn)換
注意:這里不需要掃描DTO包,但是核心配置必須使用下面注解:
<mvc:annotation-driven></mvc:annotation-driven>
這個(gè)注解的功能大于單獨(dú)的注解處理器和適配器的能力。
頁(yè)面:
時(shí)間:
<fmt:formatDate value="{user.birth}" pattern="yyyy-MM-dd"/> 顯示可選精度: 
Dto中加入 Date 類型 并加入注解。

在請(qǐng)求中使用 SimpleDateFormat進(jìn)行處理。

測(cè)試輸出:


從方法到頁(yè)面, 這里面我們可以通過(guò)jstl 里面的 fmt標(biāo)簽進(jìn)行時(shí)間的處理。
后端代碼:
@RequestMapping("six.do")
public String goToSix(Model model){
Dto dto =new Dto("張穎豪","123",new String[]{"愛(ài)啦啦啦","aixxxx是"},new Date());
model.addAttribute("six",dto);
return "/index.jsp";
}
前端代碼:
<html>
<body>
<h2>Hello World!</h2>
{six.username}
{six.password}
<c:forEach items="{six.like}" var="item">{item}
</c:forEach>
<fmt:formatDate value="${six.birth}" pattern="yyyy/MM/dd/hh:mm:ss"></fmt:formatDate>
</body>
</html>
運(yùn)行結(jié)果:

精度測(cè)試: 依照實(shí)際情況自己進(jìn)行配置
在Dto 中: 注意注解
@NumberFormat(pattern = "#,###,###.#")
private Double price;
后端代碼中,傳入一個(gè)小數(shù)點(diǎn)很長(zhǎng)的數(shù)值

在前端 jsp 界面解析的時(shí)候 :
<fmt:formatNumber value="${six.price}" pattern="#,###,###.#" maxFractionDigits="2" minFractionDigits="2"></fmt:formatNumber>
maxFractionDigits :最大小數(shù)點(diǎn)長(zhǎng)度
minFractionDigits : 最小小數(shù)點(diǎn)長(zhǎng)度
結(jié)果如下: 注意小數(shù)點(diǎn)長(zhǎng)度 為兩位 并且 數(shù)值四舍五入

嵌套綁定數(shù)據(jù)(了解)
使用JavaBean綁定數(shù)據(jù)的時(shí)候需要注意,頁(yè)面上控件的名稱和DTO中的屬性名稱一致。
注意:DTO獲取某個(gè)字段失敗,跳轉(zhuǎn)Action會(huì)失敗。
注意頁(yè)面屬性名稱有嵌套
如:
增加的用戶名:<input type="text" name="other.username">
增加的密碼:<input type="password" name="other.password">


在five.do文件里面做處理:

演示:

上面箭頭指向?yàn)榍短讓?duì)象 , 并且嵌套字段里面有兩個(gè)屬性值。

正確獲取到嵌套對(duì)象里面的屬性值。