日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

(SpringMVC)RestFul和Controller

發布時間:2025/3/20 javascript 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (SpringMVC)RestFul和Controller 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1. 控制器Controller
    • 1.1 實現Controller接口
    • 1.2 使用注解@Controller
  • 2. RestFul 風格

1. 控制器Controller

  • 控制器復雜提供訪問應用程序的行為,通常通過接口定義或注解定義兩種方法實現。

  • 控制器負責解析用戶的請求并將其轉換為一個模型。

  • 在Spring MVC中一個控制器類可以包含多個方法

  • 在Spring MVC中,對于Controller的配置方式有很多種

1.1 實現Controller接口

Controller是一個接口,在org.springframework.web.servlet.mvc包下,接口中只有一個方法;

//實現該接口的類獲得控制器功能 public interface Controller {//處理請求且返回一個模型與視圖對象ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws Exception; }

1.配置web.xml和spring配置文件
2.編寫一個Controller類,ControllerTest1

//定義控制器 //注意點:不要導錯包,實現Controller接口,重寫方法; public class ControllerTest1 implements Controller {public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {//返回一個模型視圖對象ModelAndView mv = new ModelAndView();mv.addObject("msg","Test1Controller");mv.setViewName("test");return mv;} }

3.在Spring配置文件中注冊請求的bean

<bean id="/t1" class="com.zh.controller.ControllerTest1"/>

4.根據視圖解析器的目錄編寫jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body> ${msg} </body> </html>

實現接口Controller定義控制器是較老的辦法,現在一般不用

缺點是:一個控制器中只有一個方法,如果要多個方法則需要定義多個Controller;定義的方式比較麻煩;
5.測試

1.2 使用注解@Controller

@Controller注解類型用于聲明Spring類的實例是一個控制器

Spring可以使用掃描機制來找到應用程序中所有基于注解的控制器類,為了保證Spring能找到控制器,需要在配置文件中聲明組件掃描。

<!-- 自動掃描包,讓指定包下的注解生效,由IOC容器統一管理 --><context:component-scan base-package="com.zh.controller"/>

增加一個ControllerTest2類,使用注解實現;

//@Controller注解的類會自動添加到Spring上下文中 @Controller public class ControllerTest2 {//映射訪問路徑@RequestMapping("/t2")public String test(Model model){//Spring MVC會自動實例化一個Model對象用于向視圖中傳值model.addAttribute("msg","ControllerTest2");//返回視圖位置return "test";} }

測試

2. RestFul 風格

概念

Restful就是一個資源定位及資源操作的風格。不是標準也不是協議,只是一種風格。基于這個風格設計的軟件可以更簡潔,更有層次,更易于實現緩存等機制。
功能

資源:互聯網所有的事物都可以被抽象為資源

資源操作:使用POST、DELETE、PUT、GET,使用不同方法對資源進行操作。

分別對應 添加、 刪除、修改、查詢。
傳統方式操作資源 :通過不同的參數來實現不同的效果!方法單一,post 和 get

http://127.0.0.1/item/queryItem.action?id=1 查詢,GEThttp://127.0.0.1/item/saveItem.action 新增,POSThttp://127.0.0.1/item/updateItem.action 更新,POSThttp://127.0.0.1/item/deleteItem.action?id=1 刪除,GET或POST

使用RESTful操作資源 :可以通過不同的請求方式來實現不同的效果!如下:請求地址一樣,但是功能可以不同!

http://127.0.0.1/item/1 查詢,GEThttp://127.0.0.1/item 新增,POSThttp://127.0.0.1/item 更新,PUThttp://127.0.0.1/item/1 刪除,DELETE

測試使用

@Controller public class RestFulController {@RequestMapping("/add/{a}/{b}")public String test1(@PathVariable int a,@PathVariable int b, Model model){int res = a+b;model.addAttribute("msg","結果為"+res);return "test";} }


使用路徑變量的好處

  • 使路徑變得更加簡潔;

  • 獲得參數更加方便,框架會自動進行類型轉換。

  • 通過路徑變量的類型可以約束訪問參數,如果類型不一樣,則訪問不到對應的請求方法,如這里訪問是的路徑是/commit/1/a,則路徑與方法不匹配,而不會是參數轉換失敗。

使用method屬性指定請求類型
將請求類型改為post

@Controller public class RestFulController {@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.POST)public String test1(@PathVariable int a,@PathVariable int b, Model model){int res = a+b;model.addAttribute("msg","結果為"+res);return "test";} }

報405錯誤

改為GET請求類型

@Controller public class RestFulController {@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)public String test1(@PathVariable int a,@PathVariable int b, Model model){int res = a+b;model.addAttribute("msg","結果為"+res);return "test";} }

成功

Spring MVC 的 @RequestMapping 注解能夠處理 HTTP 請求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。
所有的地址欄請求默認都會是 HTTP GET 類型的。

方法級別的注解變體有如下幾個:組合注解

@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping

@GetMapping 是一個組合注解,平時使用的會比較多!

它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一個快捷方式。

總結

以上是生活随笔為你收集整理的(SpringMVC)RestFul和Controller的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。