版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第SpringBoot自定義對(duì)象參數(shù)超詳細(xì)介紹作用目錄一、實(shí)體類Bean二、前端表單index.html三、Controller類四、運(yùn)行結(jié)果截圖問題提出一:
當(dāng)我們用表單獲取一個(gè)Person對(duì)象的所有屬性值時(shí),SpringBoot是否可以直接根據(jù)這些屬性值將其轉(zhuǎn)換為Person對(duì)象
回答:
當(dāng)然可以,SpringBoot通過自定義對(duì)象參數(shù),可以實(shí)現(xiàn)自動(dòng)類型轉(zhuǎn)換與格式化,并可以級(jí)聯(lián)封裝(一個(gè)對(duì)象擁有另一個(gè)對(duì)象作為屬性時(shí),也可以封裝)。
一、實(shí)體類Bean
person類
注:構(gòu)造方法一定要寫全,無參數(shù)和有參數(shù)的都要寫,不然封裝過程會(huì)出問題
importorg.springframework.context.annotation.Bean;
importjavax.xml.crypto.Data;
publicclassPerson{
StringuserName;
intage;
Petpet;
publicPerson(){
publicPerson(StringuserName,intage,Petpet){
this.userName=userName;
this.age=age;
this.pet=pet;
publicStringgetUserName(){
returnuserName;
publicvoidsetUserName(StringuserName){
this.userName=userName;
publicintgetAge(){
returnage;
publicvoidsetAge(intage){
this.age=age;
publicPetgetPet(){
returnpet;
publicvoidsetPet(Petpet){
this.pet=pet;
}
pet類
packagecom.example.demo2.bean;
publicclassPet{
Stringname;
Stringage;
publicPet(){
publicPet(Stringname,Stringage){
=name;
this.age=age;
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
=name;
publicStringgetAge(){
returnage;
publicvoidsetAge(Stringage){
this.age=age;
}
二、前端表單index.html
注意input的name屬性值要與類的屬性名一一對(duì)應(yīng)
!DOCTYPEhtml
htmllang="en"
head
metacharset="UTF-8"
title自定義參數(shù)綁定原理/title
/head
body
formaction="/saveuser"method="post"
姓名:inputname="userName"value="liuwanqing"/br/
年齡:inputname="age"value="20"/br/
寵物姓名:inputname=""/br/
寵物年齡:inputname="pet.age"/
inputtype="submit"value="保存"
/form
/body
/html
三、Controller類
Post/saveuser請(qǐng)求,返回封裝好的Person類
packagecom.example.demo2.controller;
importcom.example.demo2.bean.Person;
importorg.springframework.web.bind.annotation.PostMapping;
importorg.springframework.web.bind.annotation.RestController;
@RestController
publicclassParameterTestController{
@PostMapping("/saveuser")
publicPersonsaveuser(Personperson){
returnperson;
}
四、運(yùn)行結(jié)果截圖
提出問題二:SpringBoot之所以可以自動(dòng)獲取表單值封裝為指定類型對(duì)象,是因?yàn)镾pringBoot具有嚴(yán)密的參數(shù)解析機(jī)制,但是若我們的輸入值SpringBoot不能解析時(shí),難道我們就只能坐以待斃了嘛
回答:不是,我們可以通過WebMvcConfigurer定制化SpringMVC的功能,通過重寫addFormatters方法自定義類型參數(shù)
示例
如下表單中的huahua,5個(gè)月字符串是不能被SpringBoot解析為Pet類型的
formaction="/saveuser"method="post"
姓名:inputname="userName"value="liuwanqing"/br/
年齡:inputname="age"value="20"/br/
寵物:inputname="pet"value="huahua,5個(gè)月"/
inputtype="submit"value="保存"
/form
自定義類型參數(shù)封裝POJO:
編寫WebConfig類實(shí)現(xiàn)WebMvcConfigurer類,重寫addFormatters方法
packagecom.example.demo2.config;
importcom.example.demo2.bean.Pet;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.core.convert.converter.Converter;
importorg.springframework.format.FormatterRegistry;
importorg.springframework.util.StringUtils;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
publicclassWebConfigimplementsWebMvcConfigurer{
//1、WebMvcConfigurer定制化SpringMVC的功能
@Bean
publicWebMvcConfigurerwebMvcConfigurer(){
returnnewWebMvcConfigurer(){
@Override
publicvoidaddFormatters(FormatterRegistryregistry){
registry.addConverter(newConverterString,Pet(){
@Override
publicPetconvert(Stringsource){
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年江蘇省徐州市中考物理真題卷含答案解析
- 倉(cāng)庫(kù)三級(jí)安全培訓(xùn)試題(附答案)
- 2025年大數(shù)據(jù)工程師職業(yè)資格考試試題及答案
- 2025年煤礦全員復(fù)工復(fù)產(chǎn)培訓(xùn)考試題庫(kù)及答案
- 幼兒園食堂食品安全管理制度
- 游泳池突發(fā)公共衛(wèi)生事件應(yīng)急救援預(yù)案
- 年度個(gè)人年終工作總結(jié)模板及范文
- 建筑公司三級(jí)安全教育考試題(附答案)
- 2025年鄉(xiāng)村醫(yī)生年度工作總結(jié)例文(二篇)
- 名中醫(yī)工作室工作制度
- 2026年食品安全員培訓(xùn)考試模擬題庫(kù)及解析答案
- 2025年度麻醉科主任述職報(bào)告
- 第三次全國(guó)國(guó)土調(diào)查工作分類與三大類對(duì)照表
- 農(nóng)村集貿(mào)市場(chǎng)改造項(xiàng)目實(shí)施方案
- 消防設(shè)施檢查記錄表
- 酒店協(xié)議價(jià)合同
- 哈爾濱工業(yè)大學(xué)簡(jiǎn)介宣傳介紹
- 中國(guó)兒童錯(cuò)頜畸形早期矯治專家共識(shí)
- GB/T 5147-2003漁具分類、命名及代號(hào)
- GB/T 2703-2017鞋類術(shù)語
- GB/T 10125-2021人造氣氛腐蝕試驗(yàn)鹽霧試驗(yàn)
評(píng)論
0/150
提交評(píng)論