Spring Mvc中傳遞參數(shù)方法之url/requestMapping詳解
前言
相信大家在使用spring的項目中,前臺傳遞參數(shù)到后臺是經(jīng)常遇到的事, 我們必須熟練掌握一些常用的參數(shù)傳遞方式和注解的使用,本文將給大家介紹關于Spring Mvc中傳遞參數(shù)方法之url/requestMapping的相關內(nèi)容,分享出來供大家參考學習,話不多說,直接上正文。
方法如下
1. @requestMapping: 類級別和方法級別的注解, 指明前后臺解析的路徑。 有value屬性(一個參數(shù)時默認)指定url路徑解析,method屬性指定提交方式(默認為get提交)
@RequestMapping(value = "/testing") public class QuestionSetDisplayController extends BaseController {} @RequestMapping(value = "/applicant/recover") public BaseModel recover(String cellphone) throws OTPException { return userService.recover(cellphone); }
2. @RequestParam: 請求參數(shù)規(guī)則注解。 value屬性匹配前臺傳遞的參數(shù)(一個參數(shù)時默認),required屬性此字段是否必須傳值(boolean,默認為true),defaultValue此參數(shù)的默認值(存在此參數(shù)時,說明前臺不必需傳遞參數(shù),required為false)
@RequestMapping("/login") //url: /login?name=tom public String login(@RequestParam(value="age",required=false,defaultValue="24") String agenum,@RequestParam("name") String name){ return "hello"; }
3. @PathVariable: url參數(shù)注解, 一般用于從url中獲取參數(shù)
@RequestMapping(value = "/system/getAllCodeTableData/{category}", method = RequestMethod.GET) //前臺url: '/system/getAllCodeTableData/APPLICANT_ENGLISH'
public List<CodeTableModel> getCodeTableModelByCategory(@PathVariable String category) throws OTPException {<br> return codeTableService.getCodeTableModelByCategory(category); <br>}
4. 特殊的 屬性編輯器 在前臺到后臺data日期類型等的轉(zhuǎn)化會出錯,此時我們需要屬性編輯器進行屬性的轉(zhuǎn)化 //日期傳遞參數(shù)會產(chǎn)生異常,因此在傳遞時間參數(shù)時,需要進行類型轉(zhuǎn)換,在初始化時進行數(shù)據(jù)的綁定與轉(zhuǎn)化
@RequestMapping(value="/todate/{data}",method=RequestMethod.GET) public String todate(@PathVariable("data") Date date){ System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date)); return "start"; } @InitBinder //初始化參數(shù)綁定, 日期類型的轉(zhuǎn)化, public void initBinder(ServletRequestDataBinder binder){ binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true)); }
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持
相關文章
Spring中property-placeholder的使用與解析詳解
本篇文章主要介紹了Spring中property-placeholder的使用與解析詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Spring中的注解@Autowired實現(xiàn)過程全解(@Autowired 背后的故事)
這篇文章主要介紹了Spring中的注解@Autowired實現(xiàn)過程全解,給大家聊聊@Autowired 背后的故事及實現(xiàn)原理,需要的朋友可以參考下2021-07-07SpringTask-Timer實現(xiàn)定時任務的詳細代碼
在項目中開發(fā)定時任務應該一種比較常見的需求,今天通過示例代碼給大家講解SpringTask-Timer實現(xiàn)定時任務的相關知識,感興趣的朋友一起看看吧2024-06-06Java POI實現(xiàn)將導入Excel文件的示例代碼
這篇文章主要介紹了Java POI實現(xiàn)將導入Excel文件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02springboot?如何解決yml沒有spring的小葉子標志問題
這篇文章主要介紹了springboot?如何解決yml沒有spring的小葉子標志問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03關于接口ApplicationContext中的getBean()方法使用
這篇文章主要介紹了關于接口ApplicationContext中的getBean()方法使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09基于java SSM springboot實現(xiàn)抗疫物質(zhì)信息管理系統(tǒng)
這篇文章主要介紹了基于JAVA SSM springboot實現(xiàn)的抗疫物質(zhì)信息管理系統(tǒng),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08