文章目錄
- 一、Controller 配置總結
- 二、RestFul 風格
- 2.1 使用 @RequestMapping 的 method 屬性指定請求類型
- 三、擴展:小黃鴨調試法
一、Controller 配置總結
-
實現 Controller 控制器的方式
實現 Controller 接口,重寫 handleRequest 方法實現
-
控制器實現
public class ImplementsController implements Controller {@Overridepublic ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {ModelAndView modelAndView = new ModelAndView();modelAndView.addObject("msg", "通過實現關系實現控制器!!!");modelAndView.setViewName("test");return modelAndView;} }
-
Spring 配置
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 其實不使用映射器和適配器也可以實現,因為 Spring 都給處理了 --><!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>--><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean><bean id="/ImplementsController" class="com.sys.controller.ImplementsController"/> </beans>
使用注解實現控制器
-
控制器實現
@Controller @RequestMapping("/AnnotaionController") public class AnnotaionController {@RequestMapping("/annotaion")public String annotaionTest(Model model){model.addAttribute("msg" ,"通過注解實現控制器!!!");return "test";} }
-
Spring 配置
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 自動掃描包,讓指定包下的注解生效,由IOC容器統一管理 --><context:component-scan base-package="com.sys.controller"/><!-- 讓Spring MVC不處理靜態資源 --><mvc:default-servlet-handler/><!-- 支持mvc注解驅動 --><mvc:annotation-driven /><!-- 視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><!-- 前綴 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 后綴 --><property name="suffix" value=".jsp" /></bean></beans>
-
二、RestFul 風格
RestFul 風格詳解
-
什么是 RestFul 風格:
-
Restful 就是一個資源定位及資源操作的風格。不是標準也不是協議,只是一種風格。基于這個風格設計的軟件可以更簡潔,更有層次,更易于實現緩存等機制。
-
-
RestFul 風格的作用:
-
互聯網上所有的內容,都可以別成為資源。而 RestFul 風格可以通過不同的請求方式對資源進行操作。
-
-
HTTP 的請求方式:POST、DELETE、PUT、GET。
-
RestFul 風格要求每個資源都使用 URI (Universal Resource Identifier) 得到一個唯一的地址。所有資源都共享統一的接口,以便在客戶端和服務器之間傳輸狀態。使用的是標準的 HTTP 方法,比如 GET、PUT、POST 和 DELETE。
總之就是REST是一種寫法上規范,獲取數據或者資源就用GET,更新數據就用PUT,刪除數據就用DELETE,然后規定方法必須要傳入哪些參數,每個資源都有一個地址。
-
-
未使用 RestFul 風格的代碼示例
-
控制器代碼示例
@Controller @RequestMapping("/AnnotaionController") public class AnnotaionController {@RequestMapping("/annotaion")public String annotaionTest(Model model, int a, int b){int result = a + b;model.addAttribute("msg", "結果為" + result);return "test";} }
-
訪問網址:http://localhost:8080/AnnotaionController/annotaion?a=1&b=2
-
-
使用 RestFul 風格的代碼示例
-
控制器代碼示例
@Controller public class RestFulController {@RequestMapping("/restFul/{a}/{b}")public String restFul(Model model, @PathVariable int a,@PathVariable int b) {int result = a + b;model.addAttribute("msg", "結果為" + result);return "test";} }
-
訪問網址:http://localhost:8080/restFul/1/2
-
從訪問網址上可以看出,未使用 RestFul 風格的控制器的訪問網址是需要問號拼接的,而使用之后只需要用斜杠拼個參數即可,參數會通過控制器的 @PathVariable 注解進行分配。
2.1 使用 @RequestMapping 的 method 屬性指定請求類型
-
POST
@RequestMapping(value = “/hello”,method = {RequestMethod.POST})
可替換為:@PostMapping -
GET
@RequestMapping(value = “/hello”,method = {RequestMethod.GET})
可替換為:@GetMapping -
PUT
@RequestMapping(value = “/hello”,method = {RequestMethod.PUT})
可替換為:@PutMapping -
DELETE
@RequestMapping(value = “/hello”,method = {RequestMethod.DELETE})
可替換為:@DeleteMapping -
PATCH
@RequestMapping(value = “/hello”,method = {RequestMethod.PATCH})
可替換為:@PatchMapping
三、擴展:小黃鴨調試法
-
場景一:我們都有過向別人(甚至可能向完全不會編程的人)提問及解釋編程問題的經歷,但是很多時候就在我們解釋的過程中自己卻想到了問題的解決方案,然后對方卻一臉茫然。
-
場景二:你的同行跑來問你一個問題,但是當他自己把問題說完,或說到一半的時候就想出答案走了,留下一臉茫然的你。
其實上面兩種場景現象就是所謂的小黃鴨調試法(Rubber Duck Debuging),又稱橡皮鴨調試法,它是我們軟件工程中最常使用調試方法之一。
此概念據說來自《程序員修煉之道》書中的一個故事,傳說程序大師隨身攜帶一只小黃鴨,在調試代碼的時候會在桌上放上這只小黃鴨,然后詳細地向鴨子解釋每行代碼,然后很快就將問題定位修復了。