第四節 數據格式化和ModelAttribute注解的介紹


 

從來都不坦盪,情緒都寫在臉上;
不開心的時候,不愛說話,笑也勉強。

課堂筆記,如果這么寫,不僅僅是手速,還要有語速,

這樣不太適合

   

--胖先生

   

關於數據傳遞:

客戶端傳遞數據到服務端:

1.使用普通的形式

A.傳遞簡單的數據

如果是說你傳遞的數據的名稱跟控制層中的形參的名稱不一致的情況下需要使用

注解: @RequestParam()如果存在在注解的話,那么一定要傳遞對應的名稱,除非設置required="false"

個人建議是保存名稱一致

B.傳遞的數據為表單的數據

(1)使用普通的表單進行提交,那么你需要注意的是 name="類中的數據",如果是說我的類中有關聯的類型那么name="role.role_id",表單中含有name屬性才是傳遞數據

(2)使用Jquery中的表單序列化操作,該操作比較方便,但是如果是日后工作當中,會只用客戶端傳遞JSON的數據形式的字符串

2.使用的是占位符

A.使用占位符操作,那么你一般情況下需要傳遞簡單的數據,形式如下/sys/100/tomcat

控制層當中,一定為如下的格式 /sys/{id}/{name},並且對應的形參中一定要含有@PathVariable注解

B.其實占位符可以傳遞復雜的數據,但是一般情況下需要你知道如何這是正則表達式

服務端傳遞數據到客戶端:

1.一共介紹了5種形式

(1)使用原生的二階段用request進行傳遞數據,需要你再方法(HttpServletRequest)形式

(2)ModelAndView

(3)方法名(Map<String,Object> map)注意這些形參對進行實例化操作

(4)方法名(ModelMap modelMap)注意你可以使用 Ctrl+T的快捷鍵進行查看繼承的關系

(5)方法名(Model model)

(6)返回客戶端的數據為JSON的格式

JavaScript Object Navtion 輕量級的傳輸數據格式

XML

2.關於在客戶端顯示數據的形式

A.使用EL表達式和JSTL標簽

B.使用SpringMVC提供的標簽[需要你們提醒我講]

------------------------------------------------------------------------------

  

學習內容:
1.數據的格式化和@InitBinder的注解使用
2.@ModelAttribute的簡單使用
3.封裝【未完成

   

 

   

單獨傳遞數據為日期

<h2>日期傳遞</h2>

<form action="client01" method="get">

<input type="text" name="mydate" placeholder="輸入日期格式">

<input type="submit" value="提交日期數據">

</form>

一般情況下的,日期格式我們習慣於使用YYYY-MM-DD的形式
2012-12-12的這種形式,HTTP Status 400[數據轉換失敗]
@DateTimeFormat(pattern="yyyy-MM-dd")這種形式的話,那么你的客戶端只能傳遞該種形式
疑問,我想讓兩種形式2016.01.01格式?

@RequestMapping("/client01")

public ModelAndView test01(@RequestParam(name="mydate") @DateTimeFormat(pattern="yyyy-MM-dd")Date date){

System.out.println(date);

return null;

}

  

   

單獨傳遞數據為金錢

<h2>金錢傳遞</h2>

<form action="client02" method="get">

<input type="text" name="money" placeholder="輸入金錢格式">

<input type="submit" value="提交金錢數據">

</form>

關於金錢,我們一般習慣於使用表示方法為: 1,123,000.00的形式,如果工作當中涉及到金錢操作的時候,一會使用的是為BigDecimal處理金錢,還有不能使用你了解的四舍五入,需要使用銀行家的四舍五入方式

@RequestMapping("/client02")

public ModelAndView test02(@NumberFormat(pattern="#,###.##")Double money){

System.out.println(money);

return null;

}

   

疑問,我想讓兩種形式2016.01.01||2016-10-10||2014/12/12格式?

我們需要使用對數據的處理注解,@InitBinder?

我們介紹的實現方式,現在我們只是了解其使用方式:

當你有客戶數據傳遞的時候,那么我會進入含有標注@InitBinder的方法中

package com.shxt.controller;

   

import java.awt.Component;

import java.awt.Graphics;

import java.awt.Rectangle;

import java.beans.PropertyChangeListener;

import java.beans.PropertyEditor;

import java.beans.PropertyEditorSupport;

import java.io.UnsupportedEncodingException;

import java.util.Date;

import java.util.Map;

   

import javax.servlet.http.HttpServletRequest;

   

import org.springframework.format.annotation.DateTimeFormat;

import org.springframework.format.annotation.NumberFormat;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

   

import com.shxt.model.User;

   

@Controller//如果是你沒有命名,那么id=類名的首字母小寫

public class ClienDataController {

@RequestMapping("/client01")

public ModelAndView test01(@RequestParam(name="mydate") @DateTimeFormat(pattern="yyyy-MM-dd")Date date){//接收客戶端數據,進入@InitBinder注解的方法內部

System.out.println(date);

return null;

}

@RequestMapping("/client02")

public ModelAndView test02(@NumberFormat(pattern="#,###.##")Double money){//接收客戶端數據,進入@InitBinder注解的方法內部

System.out.println(money);

return null;

}

@RequestMapping("/client03")

public ModelAndView test03(){//沒有接收客戶端數據,不進入@InitBinder注解的方法內部

System.out.println("test03");

return null;

}

@RequestMapping("/client04")

public ModelAndView test04(User user){//接收客戶端數據,進入@InitBinder注解的方法內部

System.out.println("test04");

return null;

}

@RequestMapping("/client05")

public ModelAndView test05(HttpServletRequest request){//沒有接收客戶端數據,不進入@InitBinder注解的方法內部

System.out.println("test05");

return null;

}

@RequestMapping("/client06")

public ModelAndView test06(Map<String,Object> map){//沒有接收客戶端數據,不進入@InitBinder注解的方法內部

System.out.println("test06");

return null;

}

@RequestMapping("/client07")

public ModelAndView test07(String shxt) throws UnsupportedEncodingException{

//如果是GET請求解決中文亂碼問題,有兩種方式:請自己總結一下

/*System.out.println("test07---->>"+

new String(shxt.getBytes("ISO8859-1"),"UTF-8"));*/

System.out.println("test07---->>"+shxt);

return null;

}
 

@InitBinder

//當客戶端傳遞數據的時候,並且我的控制器中的方法要接收之前,會進入該標注的方法內部進行處理

publicvoid shxt(WebDataBinder binder){

System.out.println("請注意觀察該輸入語句,在上面情況下輸出?");

//String.class為客戶端傳遞的數據要轉換成形參所對應的那個類

binder.registerCustomEditor(String.class, new PropertyEditorSupport(){

//內部類-->>什么是Java內部類,如何使用

@Override

public void setAsText(String text) throws IllegalArgumentException {

System.out.println("客戶端傳遞的數據為:"+text);

setValue(text+":齊天大聖");//重新賦值

}

});

}

   

}

  

   

配置工具類,進行對日期的解決方案

@InitBinder//當客戶端傳遞數據的時候,並且我的控制器中的方法要接收之前,會進入該標注的方法內部進行處理

public void shxt(WebDataBinder binder){

binder.registerCustomEditor(Date.class, new PropertyEditorSupport(){

@Override

public void setAsText(String text) throws IllegalArgumentException {

setValue(DateUtils.parseDate(text));

}

});

}

針對於DateUtils的工具類,默認需要使用commons-lang-2.6.jar包的支持

   

//注意使用這種方式去實現,自己的工具類的方式?
//可以回去自己去實現繼承Map接口,實現一個HashMap,很好玩
public
class DateUtils
extends org.apache.commons.lang.time.DateUtils {

private static String[] parsePatterns = {

"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",

"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",

"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

   

自定義數據的轉換器如何建立,請三階段學習完之后,再做擴展

   

_______________________________________________________________________________________

   

介紹@ModelAttribute注解方式介紹[2016/4/8 星期五 21:36]

1.模擬情景,更新操作,如下圖:

   

這情景模式,就產生如下的模擬代碼:

JSP頁面代碼:
<h2>用戶更新</h2>

<form action="user/update" method="post">

<input type="hidden" name="_method" value="put">

<input type="text" name="account" value="wukong">

<input type="text" name="user_id" value="1000">

<input type="submit" value="用戶更新">

</form>

持久化類代碼,省略getter和setter

public class User {

private Integer user_id;

private String user_name;

private String account;

private String password;

   

package com.shxt.controller;

   

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

   

import com.shxt.model.User;

   

@Controller

public class UserController {

@RequestMapping(value="/user/update",method=RequestMethod.PUT)

public ModelAndView update(User user){

System.out.println(user);

return null;

}

@RequestMapping("/shxt")

public ModelAndView test01(){

System.out.println("哈哈哈");

return null;

}

@ModelAttribute

public void init2(){

System.out.println("******");

}

   

@ModelAttribute

public void init1(Integer user_id){

System.out.println("======"+user_id);

}

@InitBinder//想想這個使用規則?

public void數據(WebDataBinder binder){

System.out.println("四海興唐");

}

   

}

問題:在控制台輸出的結果是什么?
 

那么我們已經搞定了上面的代碼,下面我們來進行改造,代碼如下:

package com.shxt.controller;

   

import java.util.Map;

   

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

   

import com.shxt.model.User;

   

@Controller

public class UserController {

@RequestMapping(value="/user/update",method=RequestMethod.PUT)

public ModelAndView update(@ModelAttribute(value="user_data")User user){

System.out.println(user);

return null;

}

@RequestMapping("/shxt")

public ModelAndView test01(){

System.out.println("哈哈哈");

return null;

}

@ModelAttribute

public void init2(){

System.out.println("******");

}

   

@ModelAttribute

public void init1(Integer user_id,Map<String,Object> map){

//System.out.println("======"+user_id);

if(user_id!=null){

//查詢數據庫,模擬通過主鍵查詢數據庫操作

System.out.println("我進來了");

User user = new User();

user.setUser_id(1000);

user.setAccount("wukong");

user.setPassword("123456");

user.setUser_name("悟空");

map.put("user_data",user);

}

}

   

}

看看跟之前的測試結果又什么不同

  

 文檔下載地址:  https://yunpan.cn/cq2cam4zVjZaw  訪問密碼 fb28

   

胖先生的微信

感覺該文章對你有所幫助,請點擊下方
推薦↓↓↓↓↓↓↓↓↓↓
您的支持是我最大的動力
該資料推薦給
四海興唐的各位同學,希望你們工作順利,不管何時何地,能幫助你們是胖先生的榮幸!
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM