SpringMVC學習(二) 前後端傳值

2021-09-25 22:09:01 字數 1404 閱讀 1813

@controller

public class testcontroller

//如果名字不一樣,可以使用@requestparam註解,這個註解還可以配置預設值@requestparam(defaultvalue="")

public string test2(@requestparam(value="username")string name,@requestparam(value="password")string pwd)

//如果寫物件,屬性名要對應,springmvc會自動將資料封裝為乙個物件,也可以加上request和response,session

//當引數為集合型別,前端為main.jsp中的核取方塊

public string test4(@requestparam("hover")listlist)

//當引數為user.username,user.password,可以自動封裝為people型別

public string test5(people people)

//restful風格的引數,用起來會使請求格式統一,@pathvariable是必須加的

public string test6(@pathvariable("username")string name,@pathvariable string password )

//如果想直接返回乙個字串或json物件,可以這樣

//如果不加@responsebody,還是會實現跳轉功能,不要忘記新增jackson依賴

@responsebody

public user test8()

//如果返回值不是key-value形式(物件,map),將響應頭改為text/html

//所以如果是中文需要設定編碼

@responsebody

public string test9()

}

@controller

public class test2controller

// 第二種:使用map

public string test2(mapmap)

// 第三種:使用model,與map的方法類似

public string test3(model model)

//第四種:使用modelandview,將檢視和資料放到乙個modelandview物件中

public modelandview test4(modelandview mv)

springMVC 前後端傳值

由於在寫 的時候經常需要前後端進行傳值,那麼總結下前端是如何給後端傳值,以及後端是如何接收的。以下包括 pathvarible,pathparam,requestparam,requestbody,requestheader 以及 spring 自動封裝。前端 http localhost 8080...

SpringMVC學習筆記 前後端資料處理

1.1 提交的網域名稱稱和處理方法的引數名一致 test1 public string test1 string name,model model 1.2 提交的網域名稱稱和處理方法的引數名不一致 需要在字段前面加上 requestparam 要傳入的引數名 test2 public string ...

SpringMVC 前後端傳值總結

文章主要來自 點這裡 這也是博主的部落格,主要分享了自己接觸過的一些後端技術,有不對的地方希望可以提出。由於在寫 的時候經常需要前後端進行傳值,那麼總結下前端是如何給後端傳值,以及後端是如何接收的。以下包括 pathvarible,pathparam,requestparam,requestbody...