1- 控制器Controller
- 控制器複雜提供通路應用程式的行為,通常通過接口定義或注解定義兩種方法實作。
- 控制器負責解析使用者的請求并将其轉換為一個模型。
- 在Spring MVC中一個控制器類可以包含多個方法,在Spring MVC中,對于Controller的配置方式有很多種
1.1 實作Controller接口
1.1.1項目目錄
1.1.2 操作業務Controller
實作Controller接口,進而獲得控制器功能
package cn.guardwhy.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Controller詳解
*/
public class TestController1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws Exception {
// 1.傳回一個模型視圖對象
ModelAndView mv = new ModelAndView();
// 2.封裝對象,放在ModelAndView中。
mv.addObject("msg", "TestController1");
// 3. 封裝跳轉的視圖
mv.setViewName("test");
return mv;
}
}
1.1.3 SpringMVC的配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--注冊bean,将類送出給springIOC容器來管理-->
<bean id="/test1" class="cn.guardwhy.controller.TestController1"/>
<!--4.視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--字首-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--字尾-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
1.1.4 建立視圖層
在WEB-INF/ jsp目錄中建立hello.jsp,視圖可以直接取出并展示從Controller帶回的資訊。可以通過EL表示取出Model中存放的值,或者對象。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>guardwhy</title>
</head>
<body>
${msg}
</body>
</html>
4.1.5 執行結果
注意點
- 實作接口Controller定義控制器是較老的辦法。
- 一個控制器中隻有一個方法,如果要多個方法則需要定義多個Controller。定義的方式比較麻煩。
1.2 使用注解@Controller
1.2.1 操作業務Controller
package cn.guardwhy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 注解形式
*/
@Controller // 代表這個類會被Spring接管,被這個注解的類中的所有方法,如果傳回值是Spring
// 并且有具體頁面可以跳轉,那麼就會被視圖解析器解析。
public class TestController2 {
// 映射通路路徑
@RequestMapping("/test2")
public String Test(Model model){
// 自動執行個體化一個Model對象用于向視圖中傳值
model.addAttribute("msg", "ControllerTest2");
return "test"; // 會被拼接成//WEB-INF//jsp//test.jsp
}
}
1.2.2 SpringMVC的配置檔案
<?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"
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">
<!-- 自動掃描指定的包,下面所有注解類交給IOC容器管理 -->
<context:component-scan base-package="cn.guardwhy.controller"/>
<!--4.視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--字首-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--字尾-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
1.2.3 執行結果
1.3 RequestMapping
@RequestMapping注解用于映射url到控制器類或一個特定的處理程式方法。
可用于類或方法上,用于類上,表示類中的所有響應請求的方法都是以該位址作為父路徑。
1.3.1 注解在方法上
package cn.guardwhy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController3 {
@RequestMapping("/test3")
public String Test3(Model model){
// 自動執行個體化一個Model對象用于向視圖中傳值
model.addAttribute("msg", "ControllerTest3");
return "test";
}
}
1.3.2 同時注解類與方法
package cn.guardwhy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/guardwhy")
public class TestController3 {
@RequestMapping("/test3")
public String Test3(Model model){
// 自動執行個體化一個Model對象用于向視圖中傳值
model.addAttribute("msg", "ControllerTest3");
return "test";
}
}
1.4 RestFul風格
Restful就是一個資源定位及資源操作的風格。不是标準也不是協定,隻是一種風格。基于這個風格設計的軟體可以更簡潔,更有層次,更易于實作緩存等機制。
1.4.1 項目目錄
1.4.2 基本作用
資源:網際網路所有的事物都可以被抽象為資源。
資源操作:使用POST、DELETE、PUT、GET,使用不同方法對資源進行操作。分别對應 添加、 删除、修改、查詢。
傳統方式操作資源
通過不同的參數來實作不同的效果!!!
- http://localhost:8080/query.stu?id=1 查詢,GET
- http://localhost:8080/save.stu 新增,POST
- http://localhost:8080/update.stu 更新,POST
- http://localhost:8080/delete.stu?id=1 删除 POST
使用RESTful操作資源
可以通過不同的請求方式來實作不同的效果!!注意,請求位址一樣,但是功能可以不同!!!
- http://localhost:8080/stu/1 查詢,GET
- http://localhost:8080/stu 新增,POST
- http://localhost:8080/stu 更新,PUT
- http://localhost:8080/stu/1 删除 DELETE
1.4.3 控制器(Controller)
在Spring MVC中可以使用@PathVariable 注解,讓方法參數的值對應綁定到一個URL模闆變量。
package cn.guardwhy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* RestFul風格入門案例
*/
@Controller
public class RestFulController {
// 1.通路映射路徑
@RequestMapping(value = "/commit/{a}/{b}", method = RequestMethod.GET)
public String test1(@PathVariable int a, @PathVariable String b, Model model){
// 傳回結果集
String res = a + b;
model.addAttribute("msg", "結果傳回值1:" + res);
return "success";
}
// post方法
@PostMapping("/commit/{a}/{b}")
public String test2(@PathVariable int a, @PathVariable String b, Model model){
// 傳回結果集
String res = a + b;
model.addAttribute("msg", "結果傳回值2:" + res);
return "success";
}
}
視圖層
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>post送出</title>
</head>
<body>
<form action="commit/1/6" method="post">
<input type="submit" value="送出">
</form>
</body>
</html>
1.4.4 執行結果
1.4.5 總結
Spring MVC 的 @RequestMapping 注解能夠處理 HTTP 請求的方法, 比如 GET, PUT, POST, DELETE。所有的位址欄請求預設都會是 HTTP GET 類型的。
組合注解:
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping