四、Controller 配置总结、RestFul 风格




一、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 风格的作用:

    • 互联网上所有的内容,都可以别成为资源。而 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),又称橡皮鸭调试法,它是我们软件工程中最常使用调试方法之一。

在这里插入图片描述

此概念据说来自《程序员修炼之道》书中的一个故事,传说程序大师随身携带一只小黄鸭,在调试代码的时候会在桌上放上这只小黄鸭,然后详细地向鸭子解释每行代码,然后很快就将问题定位修复了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将一个 HTML 项目整合到 Spring Boot 中,需要使用 Spring Boot 的 Web 模块,同时使用 Thymeleaf 模板引擎来渲染 HTML 页面。接下来,可以通过设置 `@RestController` 注解来实现 RESTful 风格的 API 接口。 以下是一些主要步骤: 1. 添加 Spring Boot 的 Web 和 Thymeleaf 依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 创建一个控制器类来处理请求和响应: ```java @RestController @RequestMapping("/api") public class ApiController { @GetMapping("/hello") public String hello() { return "Hello, World!"; } } ``` 3. 创建一个 Thymeleaf 模板来渲染 HTML 页面: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>My HTML Page</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 4. 创建一个控制器类来返回 HTML 页面: ```java @Controller public class HtmlController { @GetMapping("/") public String index(Model model) { model.addAttribute("message", "Hello, World!"); return "index"; } } ``` 5. 在 `application.properties` 文件中添加 Thymeleaf 的配置: ``` spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 现在,可以通过访问 `http://localhost:8080/api/hello` 来获取一个 JSON 格式的响应,也可以通过访问 `http://localhost:8080/` 来获取一个 HTML 页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值