文章目录
- 一、Controller 配置总结
- 二、RestFul 风格
- 2.1 使用 @RequestMapping 的 method 属性指定请求类型
- 三、扩展:小黄鸭调试法
一、Controller 配置总结
- 实现 Controller 控制器的方式
实现 Controller 接口,重写 handleRequest 方法实现
- 控制器实现
public class ImplementsController implements Controller {
@Override
public 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/beans
http://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/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
https://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 风格可以通过不同的请求方式对资源进行操作。
- 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),又称橡皮鸭调试法,它是我们软件工程中最常使用调试方法之一。
此概念据说来自《程序员修炼之道》书中的一个故事,传说程序大师随身携带一只小黄鸭,在调试代码的时候会在桌上放上这只小黄鸭,然后详细地向鸭子解释每行代码,然后很快就将问题定位修复了。