欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Mvc中傳遞參數(shù)方法之url/requestMapping詳解

 更新時間:2017年07月31日 10:26:03   作者:果感  
在開發(fā)中,參數(shù)傳遞是必不可少的一個功能,下面這篇文章主要給大家介紹了關于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)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持

相關文章

最新評論