首頁 > 軟體

SpringMVC-03 RestFul和控制器

2021-03-07 16:00:36

SpringMVC-03 RestFul和控制器

控制器Controller

  • 控制器複雜提供存取應用程式的行為,通常通過介面定義或註解定義兩種方法實現。
  • 控制器負責解析使用者的請求並將其轉換為一個模型。
  • 在Spring MVC中一個控制器類可以包含多個方法
  • 在Spring MVC中,對於Controller的設定方式有很多種

RequestMapping

註解方式是平時使用的最多的方式!

@RequestMapping

  • @RequestMapping註解用於對映url到控制器類或一個特定的處理程式方法。可用於類或方法上。用於類上,表示類中的所有響應請求的方法都是以該地址作為父路徑。

  • 為了測試結論更加準確,我們可以加上一個專案名測試

    只註解在方法上面

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

存取路徑:http://localhost:8080 / 專案名 / h1

同時註解類與方法

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

存取路徑:http://localhost:8080 / 專案名/ admin /h1

RestFul 風格

1.概念

Restful就是一個資源定位及資源操作的風格。不是標準也不是協定,只是一種風格。基於這個風格設計的軟體可以更簡潔,更有層次,更易於實現快取等機制。

2.功能

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

資源操作:使用POSTDELETEPUTGET,使用不同方法對資源進行操作。

分別對應 新增、 刪除、修改、查詢。

傳統方式操作資源 :通過不同的引數來實現不同的效果,方法單一,post 和 get

http://localhost:8080/item/queryItem.action?id=1 查詢,GET

http://localhost:8080/item/saveItem.action 新增,POST

http://localhost:8080/item/updateItem.action 更新,POST

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

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

http://localhost:8080/item/1 查詢,GET

http://localhost:8080/item 新增,POST

http://localhost:8080/item 更新,PUT

http://localhost:8080/item/1 刪除,DELETE

3.案例測試

3.1 編寫web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

3.2 建立springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="Controller"/>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <!--    新增 檢視解析器    -->
    <!--檢視解析器:DispatcherServlet給他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--字首-->
        <property name="prefix" value="/jsp/"/>
        <!--字尾-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

3.3 新建一個Controller類

@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", "結果:"+result);
        //返回檢視位置
        return "test";
    }
}

3.4 建立test.jsp

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

3.5 設定Tomcat,測試

個人部落格為:
MoYu's Github Blog
MoYu's Gitee Blog


IT145.com E-mail:sddin#qq.com