SpringmvcController和RestFul原理的示例分析

這篇文章將為大家詳細講解有關(guān)Spring mvc Controller和RestFul原理的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司是專業(yè)的中原網(wǎng)站建設(shè)公司,中原接單;提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行中原網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

控制器Controller

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

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

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

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

實現(xiàn)Controller接口

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

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

測試

編寫一個Controller類,ControllerTest1

// 定義控制器
// 注意點:不要導錯包,實現(xiàn)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;
  }
}

編寫完畢后,去Spring配置文件中注冊請求的bean;name對應(yīng)請求路徑,class對應(yīng)處理請求的類

<bean name="/t1" class="com.xiaohua.controller.ControllerTest1"/>

編寫前端test.jsp,注意在WEB-INF/jsp目錄下編寫,對應(yīng)我們的視圖解析器

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

配置Tomcat運行測試,我這里沒有項目發(fā)布名配置的就是一個 / ,所以請求不用加項目名,OK!

說明:

實現(xiàn)接口Controller定義控制器是較老的辦法

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

使用注解@Controller

@Controller注解類型用于聲明Spring類的實例是一個控制器(在講IOC時還提到了另外3個注解);

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

<!-- 自動掃描指定的包,下面所有注解類交給IOC容器管理 -->
<context:component-scan base-package="com.xiaohua.controller"/>

增加一個ControllerTest2類,使用注解實現(xiàn);

// @Controller注解的類會自動添加到Spring上下文中
@Controller
public class ControllerTest2{

  //映射訪問路徑
  @RequestMapping("/t2")
  public String index(Model model){
    //Spring MVC會自動實例化一個Model對象用于向視圖中傳值
    model.addAttribute("msg", "ControllerTest2");
    //返回視圖位置
    return "test";
  }

}

運行tomcat測試

可以發(fā)現(xiàn),我們的兩個請求都可以指向一個視圖,但是頁面結(jié)果的結(jié)果是不一樣的,從這里可以看出視圖是被復用的,而控制器與視圖之間是弱偶合關(guān)系。

RequestMapping
@RequestMapping注解用于映射url到控制器類或一個特定的處理程序方法??捎糜陬惢蚍椒ㄉ?。用于類上,表示類中的所有響應(yīng)請求的方法都是以該地址作為父路徑。

為了測試結(jié)論更加準確,我們可以加上一個項目名測試 myweb

只注解在方法上面

@Controller
public class TestController {
  @RequestMapping("/h2")
  public String test(){
    return "test";
  }
}

訪問路徑:http://localhost:8080 / 項目名 / h2

同時注解類與方法

@Controller
@RequestMapping("/admin")
public class TestController {
  @RequestMapping("/h2")
  public String test(){
    return "test";
  }
}

訪問路徑:http://localhost:8080 / 項目名/ admin /h2 , 需要先指定類的路徑再指定方法的路徑;

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

功能

  • 資源:互聯(lián)網(wǎng)所有的事物都可以被抽象為資源

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

  • 分別對應(yīng) 添加、 刪除、修改、查詢。

傳統(tǒng)方式操作資源 :通過不同的參數(shù)來實現(xiàn)不同的效果!方法單一,post 和 get

  • http://127.0.0.1/item/queryItem.action?id=1 查詢,GET

  • http://127.0.0.1/item/saveItem.action 新增,POST

  • http://127.0.0.1/item/updateItem.action 更新,POST

  • http://127.0.0.1/item/deleteItem.action?id=1 刪除,GET或POST

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

  • http://127.0.0.1/item/1 查詢,GET

  • http://127.0.0.1/item 新增,POST

  • http://127.0.0.1/item 更新,PUT

  • http://127.0.0.1/item/1 刪除,DELETE

學習測試

在新建一個類 RestFulController

@Controller
public class RestFulController {
}

在Spring MVC中可以使用 @PathVariable 注解,讓方法參數(shù)的值對應(yīng)綁定到一個URI模板變量上。

@Controller
public class RestFulController {

  //映射訪問路徑
  @RequestMapping("/commit/{p1}/{p2}")
  public String index(@PathVariable int p1, @PathVariable int p2, Model model){

    int result = p1+p2;
    //Spring MVC會自動實例化一個Model對象用于向視圖中傳值
    model.addAttribute("msg", "結(jié)果:"+result);
    //返回視圖位置
    return "test";

  }

}

我們來測試請求查看下

使用路徑變量的好處?

  • 使路徑變得更加簡潔;

  • 獲得參數(shù)更加方便,框架會自動進行類型轉(zhuǎn)換。

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

我們來修改下對應(yīng)的參數(shù)類型,再次測試

//映射訪問路徑
@RequestMapping("/commit/{p1}/{p2}")
public String index(@PathVariable int p1, @PathVariable String p2, Model model){

  String result = p1+p2;
  //Spring MVC會自動實例化一個Model對象用于向視圖中傳值
  model.addAttribute("msg", "結(jié)果:"+result);
  //返回視圖位置
  return "test";

}

使用method屬性指定請求類型

用于約束請求的類型,可以收窄請求范圍。指定請求謂詞的類型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等

我們來測試一下:

增加一個方法

//映射訪問路徑,必須是POST請求
@RequestMapping(value = "/hello",method = {RequestMethod.POST})
public String index2(Model model){
  model.addAttribute("msg", "hello!");
  return "test";
}

我們使用瀏覽器地址欄進行訪問默認是Get請求,會報錯405:

如果將POST修改為GET則正常了;

//映射訪問路徑,必須是Get請求
@RequestMapping(value = "/hello",method = {RequestMethod.GET})
public String index2(Model model){
  model.addAttribute("msg", "hello!");
  return "test";
}

小結(jié):

Spring MVC 的 @RequestMapping 注解能夠處理 HTTP 請求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。

所有的地址欄請求默認都會是 HTTP GET 類型的。

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

  • @GetMapping

  • @PostMapping

  • @PutMapping

  • @DeleteMapping

  • @PatchMapping

  • @GetMapping 是一個組合注解

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

關(guān)于“Spring mvc Controller和RestFul原理的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

網(wǎng)站題目:SpringmvcController和RestFul原理的示例分析
新聞來源:http://www.muchs.cn/article4/iidgoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊App開發(fā)、網(wǎng)站改版、服務(wù)器托管、手機網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作