Spring MVC主要有三個(gè)功能:
- 連接
- 獲取參數(shù)
- 輸出數(shù)據(jù)
對于 Spring MVC 來說,掌握了以上 3 個(gè)功能就相當(dāng)于掌握了Spring MVC。
1.連接
連接的功能:將?戶(瀏覽器)和 Java 程序連接起來,也就是訪問?個(gè)地址能夠調(diào)?到我們的Spring 程序。
1.1 實(shí)現(xiàn) Spring MVC的連接
先創(chuàng)建一個(gè)SpringMVC項(xiàng)目,過程和SpringBoot項(xiàng)目創(chuàng)建相同。
可以參考這篇文章SpringBoot的創(chuàng)建和運(yùn)行
接下來,創(chuàng)建?個(gè) UserController 類,實(shí)現(xiàn)?戶到 Spring 程序的互聯(lián)互通,具體實(shí)現(xiàn)代碼如下:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller // 讓 spring 框架啟動時(shí),加載
@ResponseBody // 返回???數(shù)據(jù)
@RequestMapping("/user") // 路由器規(guī)則注冊
public class UserController {
// 路由器規(guī)則注冊
@RequestMapping("/hi")
public String sayHi(){
return "<h1>Hi,Spring MVC.</h1>";
}
}
這樣實(shí)現(xiàn)之后,當(dāng)訪問地址:http://localhost:8080/user/hi 時(shí)就能打印“hello,spring mvc”的信息了。
1.2 注解介紹
@RequestMapping
@RequestMapping是?來注冊接?的路由映射的。
路由映射:所謂的路由映射指的是,當(dāng)?戶訪問?個(gè) url 時(shí),將?戶的請求對應(yīng)到程序中某個(gè)類 的某個(gè)?法的過程就叫路由映射
- @RequestMapping 即可修飾類,也可以修飾?法,當(dāng)修飾類和?法時(shí),訪問的地址是類 + ?法。
- @RequestMapping 既是post請求又是get請求。
@Controller
@RequestMapping("/web")
public class WebController {
//響應(yīng)視圖
@RequestMapping("/index")
public String index(){
return "/index.html";
}
//響應(yīng)正文
@ResponseBody
@RequestMapping("/indexData")
//只支持get請求
//@RequestMapping(value = "/indexData",method = RequestMethod.GET)
public String indexData(){
return "hello,MVC";
}
}
@GetMapping
實(shí)現(xiàn)get請求。
get請求的三種寫法:
// 寫法1
@RequestMapping("/index")
// 寫法2
@RequestMapping(value = "/index",method = RequestMethod.GET)
// 寫法3
@GetMapping("/index")
@PostMapping
實(shí)現(xiàn)post請求。
post請求的兩種寫法:
// 寫法1
@RequestMapping(value = "/index",method = RequestMethod.POST)
// 寫法2
@PostMapping("/index")
2.獲取參數(shù)
2.1 傳遞單個(gè)參數(shù)
在 Spring MVC 中可以直接??法中的參數(shù)來實(shí)現(xiàn)傳參,?如以下代碼:
@RestController
@RequestMapping("/param")
public class ParamController {
//servlet傳參
@RequestMapping("/get1")
public String get1(HttpServletRequest request){
String name=request.getParameter("name");
return "name:"+name;
}
//MVC傳參
@RequestMapping("/get2")
public String get2(String name){
return "name:"+name;
}
//多個(gè)參數(shù)傳參
@RequestMapping("/get3")
public String get3(String name,Integer age){
return "name:"+name+"|age:"+age;
}
}
2.2 傳遞對象
定義一個(gè)Student類:
@Data
public class Student {
private Integer id;
private String name;
private Integer age;
}
傳遞對象代碼實(shí)現(xiàn):
@RequestMapping("/get6")
public String get6(Student student){
return student.toString();
}
2.3 后端參數(shù)重命名
某些特殊的情況下,前端傳遞的參數(shù) key 和我們后端接收的 key 可以不?致,?如前端傳遞了?個(gè)
n 給后端,?后端?是由name 字段來接收的,這樣就會出現(xiàn)參數(shù)接收不到的情況,如果出現(xiàn)
這種情況,我們就可以使? @RequestParam 來重命名前后端的參數(shù)值。
代碼實(shí)現(xiàn):文章來源:http://www.zghlxwxcb.cn/news/detail-601911.html
@RequestMapping("/get7")
public String get7(@RequestParam(name="n",required = false) String name){
return "name:"+name;
}
參數(shù)必傳:將required設(shè)置成false,來避免不傳遞時(shí)報(bào)錯(cuò)
2.4 接收J(rèn)SON對象
@RequestBody
上傳一個(gè)json格式的數(shù)據(jù):
后端接收代碼實(shí)現(xiàn):
@RequestMapping("/get8")
public String get8(@RequestBody Student student){
log.info(student.toString());
return student.toString();
}
@RequestBody表示接收的是一個(gè)json字符串,String會幫我們把這個(gè)字符串轉(zhuǎn)為對象。
2.5 獲取URL中參數(shù)
@PathVariable
給定一個(gè)URL:
實(shí)現(xiàn)代碼:
@RequestMapping("/get9/{shopid}")
public String get9(@PathVariable Integer shopid){
return "shopid:"+shopid;
}
2.6 上傳文件
@RequestPart
實(shí)現(xiàn)代碼:
@RequestMapping("/get10")
public String get10(@RequestPart("file") MultipartFile file) throws IOException {
log.info(file.getOriginalFilename());
file.transferTo(new File("D:\\Desktop\\Bit\\temp"+file.getOriginalFilename()));
return "success";
}
2.7 獲取Cookie/Session/header
@CookieValue
獲取Session實(shí)現(xiàn)代碼:
@RequestMapping("/get11")
public String get11(@CookieValue(required = false) String bite){
return "bite:"+bite;
}
@SessionAttribute
設(shè)置Session實(shí)現(xiàn)代碼:
@RequestMapping("/get12")
public String get12(@SessionAttribute(required = false) String username){
return "username:"+username;
}
設(shè)置之后,cookie中會多一個(gè)sessionId。
@RequestHeader
獲取Header內(nèi)容實(shí)現(xiàn)代碼:
@RequestMapping("/get13")
public String get13(@RequestHeader("User-Agent") String useragent){
return "useragent:"+useragent;
}
2.8 返回JSON對象
實(shí)現(xiàn)代碼:
@RequestMapping("/get14")
public Map<String,String> get14(){
Map<String,String>map=new HashMap<>();
map.put("k1","v1");
map.put("k2","v2");
map.put("k3","v3");
map.put("k4","v4");
return map;
}
3.輸出數(shù)據(jù)
3.1 返回靜態(tài)頁面
實(shí)現(xiàn)代碼:
//響應(yīng)視圖
@RequestMapping("/index")
public String index(){
return "/index.html";
}
3.2 返回text正文
@ResponseBody
實(shí)現(xiàn)代碼:
//響應(yīng)正文
@ResponseBody
> 這里是引用
@RequestMapping("/indexData")
public String indexData(){
return "hello,MVC";
}
}
4.請求轉(zhuǎn)發(fā)和請求重定向
return 不但可以返回?個(gè)視圖,還可以實(shí)現(xiàn)跳轉(zhuǎn),跳轉(zhuǎn)的?式有兩種:
- forward:請求轉(zhuǎn)發(fā);
- redirect:請求重定向。
舉例說明: forward(請求轉(zhuǎn)發(fā))和 redirect(請求重定向)的區(qū)別
你告訴你媽媽,你想吃辣條,如果你媽媽,說好,我?guī)湍闳ベI,這就是 forward 請求轉(zhuǎn)發(fā);如果你媽媽讓你??去買,那么就是
請求 redirect 重定向
4.1 請求轉(zhuǎn)發(fā)forward
代碼實(shí)現(xiàn):
/**
* 請求轉(zhuǎn)發(fā)
* @return
*/
@RequestMapping("/forward")
public String forward(){
return "forward:/index.html";
}
4.2 請求重定向redirect
代碼實(shí)現(xiàn):文章來源地址http://www.zghlxwxcb.cn/news/detail-601911.html
/**
* 請求重定向
* @return
*/
@RequestMapping("/redirect")
public String redirect(){
return "redirect:/index.html";
}
到了這里,關(guān)于【Spring MVC】Spring MVC的功能使用和相關(guān)注解介紹的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!