• RestFul和控制器


    小提示: 

    • 如果是400的错误就是请求出了问题
    • 如果是500的错误就是代码出了问题
    • 如果出现405就是HTTP使用的method类型不对导致的

     

    1、控制器(Controller)

    • 控制器负责提供访问应用程序的行为,通常通过接口定义注解定义两种方法实现

    • 控制器负责解析用户的请求并将其转换为一个模型

    • 在SpringMVC中的一个控制器类可以包含多个方法

    • 在SpringMVC中对于控制器的配置方式有很多种

    • M模型(Model )    V视图(View)     C控制(Controller)

    1.1、实现Controller接口

    (1)新建一个Moudle,springmvc-04-controller 添加web项目,添加依赖,在WEB-INF下面创建jsp包

     

     

    (2) 配置web.xml

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <servlet>
    7. <servlet-name>springmvcservlet-name>
    8. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    9. <init-param>
    10. <param-name>contextConfigLocationparam-name>
    11. <param-value>classpath:springmvc-servlet.xmlparam-value>
    12. init-param>
    13. <load-on-startup>1load-on-startup>
    14. servlet>
    15. <servlet-mapping>
    16. <servlet-name>springmvcservlet-name>
    17. <url-pattern>/url-pattern>
    18. servlet-mapping>
    19. web-app>

     

    (3)创建springmvc-servlet.xml,添加Spring配置文件springmvc-servlet.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/context
    9. https://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.springframework.org/schema/mvc
    11. https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12. <context:component-scan base-package="com.gt.controller"/>
    13. <mvc:default-servlet-handler/>
    14. <mvc:annotation-driven/>
    15. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
    16. <property name="prefix" value="/WEB-INF/jsp/"/>
    17. <property name="suffix" value=".jsp"/>
  • 相关阅读:
    Golang | Leetcode Golang题解之第49题字母异位词分组
    力扣:5-最长回文子串
    感觉的定义
    5. Layui数据表格的快速使用
    Linux 下获取进程所在文件的路径
    【图像处理OpenCV(C++版)】——初学OpenCV
    地平线 旭日X3 PI (二) 开发机WSL Docker配置
    Win11如何给应用换图标?Win11给应用换图标的方法
    【C++课程设计——演讲比赛系统】
    系列文章|云原生时代下微服务架构进阶之路 - Spring Native
  • 原文地址:https://blog.csdn.net/qq_46423017/article/details/126726189