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

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

 更新時(shí)間:2017年07月31日 10:26:03   作者:果感  
在開發(fā)中,參數(shù)傳遞是必不可少的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于Spring Mvc中傳遞參數(shù)方法之url/requestMapping的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。

前言

相信大家在使用spring的項(xiàng)目中,前臺(tái)傳遞參數(shù)到后臺(tái)是經(jīng)常遇到的事, 我們必須熟練掌握一些常用的參數(shù)傳遞方式和注解的使用,本文將給大家介紹關(guān)于Spring Mvc中傳遞參數(shù)方法之url/requestMapping的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),話不多說,直接上正文。

方法如下

1. @requestMapping: 類級(jí)別和方法級(jí)別的注解, 指明前后臺(tái)解析的路徑。 有value屬性(一個(gè)參數(shù)時(shí)默認(rèn))指定url路徑解析,method屬性指定提交方式(默認(rèn)為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: 請(qǐng)求參數(shù)規(guī)則注解。 value屬性匹配前臺(tái)傳遞的參數(shù)(一個(gè)參數(shù)時(shí)默認(rèn)),required屬性此字段是否必須傳值(boolean,默認(rèn)為true),defaultValue此參數(shù)的默認(rèn)值(存在此參數(shù)時(shí),說明前臺(tái)不必需傳遞參數(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) //前臺(tái)url: '/system/getAllCodeTableData/APPLICANT_ENGLISH' 
public List<CodeTableModel> getCodeTableModelByCategory(@PathVariable String category) throws OTPException {<br>     return codeTableService.getCodeTableModelByCategory(category); <br>} 

4. 特殊的 屬性編輯器 在前臺(tái)到后臺(tái)data日期類型等的轉(zhuǎn)化會(huì)出錯(cuò),此時(shí)我們需要屬性編輯器進(jìn)行屬性的轉(zhuǎn)化 //日期傳遞參數(shù)會(huì)產(chǎn)生異常,因此在傳遞時(shí)間參數(shù)時(shí),需要進(jìn)行類型轉(zhuǎn)換,在初始化時(shí)進(jì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));

 } 

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持

相關(guān)文章

最新評(píng)論