當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC的请求参数,类型转换器(日期格式),请求映射
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC的请求参数,类型转换器(日期格式),请求映射
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
SpringMVC的請求參數(shù),類型轉(zhuǎn)換器(日期格式),請求映射
請求參數(shù)
springMVC將傳遞的參數(shù)封裝到處理的方法形參中,達(dá)到快速訪問參數(shù)的目的
普通參數(shù)類型
參數(shù)名與處理器方法形參名保持一致
//地址欄傳遞參數(shù)name//http://localhost:82/request/str?name=小付@RequestMapping("/str")public String test01(String name){System.out.println("地址欄傳遞的參數(shù):"+name);return "/success";} //restful風(fēng)格地址傳遞參數(shù)//http://localhost:82/request/str/小付@RequestMapping("/str/{id}")public String test02(@PathVariable("id") int id){System.out.println("restful風(fēng)格地址欄傳遞的參數(shù):"+id);return "/success";}@RequestParam注解,設(shè)定參數(shù)
POJO類型參數(shù)
實(shí)體類中使用簡單類型屬性時候,參數(shù)名與實(shí)體類中屬性名保持一致即可
pojo類型屬性,與方法其他形參同名時
復(fù)雜POJO類型參數(shù)
數(shù)組類型的參數(shù)
類型轉(zhuǎn)換器
SpringMVC對接收的數(shù)據(jù)進(jìn)行自動類型轉(zhuǎn)換,該工作通過Converter接口實(shí)現(xiàn)
日期類型格式轉(zhuǎn)換
聲明自定義的格式轉(zhuǎn)換并覆蓋系統(tǒng)的轉(zhuǎn)換格式
<!--5.啟用自定義Converter--><mvc:annotation-driven conversion-service="conversionService"/><!--1.設(shè)定格式類型Converter,注冊為Bean,受SpringMVC管理--><bean id="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><!--2.自定義Converter格式類型設(shè)定,該設(shè)定使用的是同類型覆蓋的思想--><property name="formatters"><!--3.使用set保障相同類型的轉(zhuǎn)換器僅保留一個,避免沖突--><set><!--4.設(shè)置具體的格式類型--><bean class="org.springframework.format.datetime.DateFormatter"><!--5.類型規(guī)則--><property name="pattern" value="yyyy-MM-dd"/></bean></set></property></bean>日期格式類型轉(zhuǎn)換注解方式
//傳遞時間格式,在xml配置轉(zhuǎn)換器,是spring定義的日期格式轉(zhuǎn)換器//http://localhost:82/request/date/xml?date=2020-02-02@RequestMapping("/date/xml")public String test06(Date date){System.out.println("地址欄傳遞的數(shù)據(jù)封裝為date:"+date);//地址欄傳遞的數(shù)據(jù)封裝為date:Sun Feb 02 00:00:00 CST 2020return "/success";}//傳遞時間格式,注解的方式使用@DateTimeFormat的轉(zhuǎn)換器,是spring定義的日期格式轉(zhuǎn)換器//http://localhost:82/request/date?date=2020-02-02@RequestMapping("/date")public String test07(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){System.out.println("地址欄傳遞的數(shù)據(jù)封裝為date:"+date);//地址欄傳遞的數(shù)據(jù)封裝為date:Sun Feb 02 00:00:00 CST 2020return "/success";}@RequestMapping注解,請求映射
@ReuqestMapping屬性
總結(jié)
以上是生活随笔為你收集整理的SpringMVC的请求参数,类型转换器(日期格式),请求映射的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringMVC基础配置与简单的Spr
- 下一篇: SpringMVC响应的方式,无数据跳转