Spring的核心IOC+DI AOP 注解

    V:
    C: 请求 响应 SpringMVC(请求的分析过程—控制层的请求分发) Spring对象的管理
    M: Service
    DAO MyBatis(JDBC)
    =================================================

    1.浏览器发送请求(String)
    2.到达服务端
    3.服务端接收请求(String)
    4.服务端解析请求(String 请求名字 参数 。。。。)
    5.参考配置文件 Tomcat中的那个文件 web.xml
    文件中配置了 请求—-真实类(Servlet)之间的关系
    6.通过参考配置文件找到了一个Servlet
    Servlet不能随意写,需要遵循规则
    继承HttpServlet
    重写service/doPost/doGet
    参数HttpServletRequest HttpServletResponse
    异常ServletException IOException
    方法内部做的事情 取参数 找业务 给响应
    7.响应回去,浏览器再解析,展示
    ——————————————————————————-
    以上这个是最基本的请求与响应流程
    出现了Servlet问题
    Servlet需要遵循很多的规则(Tomcat)
    一个请求(功能点)直接对应一个Servlet类
    每一个Servlet类还需要配置一个信息 8行 1行
    自己在这个问题出现的时候做了一个封装WEB框架

    一个请求 对应 一个类
    一个请求 对应 一个类中的某一个方法

    所有的Servlet请求先到达一个统一的位置DispatcherServlet
    这个类的目的是为了分析请求的类名及方法名
    根据类名和方法名找到具体执行的方法,从而执行
    *.do

    xml核心文件中就做一次配置就可以啦

    DispatcherServlet

    .do
    ——————————————————————————-
    DispatcherServlet
    Handler启动时候读取配置文件 解析 。。。
    1.接收信息 类名 方法名 参数
    2.通过类名及方法名找资源(真实的Servlet—-已经没有规则啦)
    类名+方法名———真实类下的方法对应关系???
    配置.properties 注解@RequestMapping
    3.可以发送请求
    找到一个真实类中的方法Servlet类的方法
    取值(参数) 找业务 存储数据 响应信息
    不想出现任何跟原来有关系的变量 request response
    IOC+DI
    参数 正常变量即可
    返回值 String ModelAndView
    ================================================================================
    自己封装的MVC小框架怎么用来着?
    1.下载 导包
    2.核心配置文件 web.xml
    DispatcherServlet
    .do
    3.自己写正常的控制层代码(Servlet没有规则)
    不需要继承
    不需要重写 方法名自定义
    参数不需要request response 参数目的是为了接收请求信息 请求中的key对应即可
    不需要异常
    方法里面正常做事 ModelAndView @ResponseBody @SessionAttributes
    4.发送请求的时候
    请求的名字——类中方法直接对应
    Servlet的方法的上面直接添加注解@RequestMapping(“xxx.do”)

    /按照路径来匹配的——当前工程 jsp html servlet
    .do按照请求的资源类型匹配的

    Filter —- 拦截器
    ———————————————————————————-
    参考如上的用法来大概猜测—-尝试搭建SpringMVC — 需要基于Spring使用,不能单独使用
    1.下载
    2.导包 基于spring原有的包 core contenxt beans expression
    commons-logging
    基础上多导3个包 web webmvc aop
    3.核心配置文件 web.xml
    DispatcherServlet
    *.do
    4.自己写控制层类啦
    是否有规则?
    。。。。
    参数如何接收 单个 map domain 要不要注解
    如何存储 request.setAttribute();
    响应信息 String ModelAndView JSON
    拦截器 文件上传下载

    web.xml

    1. <?xml version="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>mvc</servlet-name>
    8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    9. <init-param>
    10. <param-name>contextConfigLocation</param-name>
    11. <param-value>classpath:ApplicationContext.xml</param-value>
    12. </init-param>
    13. </servlet>
    14. <servlet-mapping>
    15. <servlet-name>mvc</servlet-name>
    16. <url-pattern>*.do</url-pattern>
    17. </servlet-mapping>
    18. </web-app>
    1. package controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestParam;
    5. import org.springframework.web.servlet.ModelAndView;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. /**
    9. * 1.继承HttpServlet 实现Controller
    10. * 2.重写service 重写
    11. * 3.两个参数 req resp 参数
    12. * 4.两个异常 serv io 异常
    13. * 5.没有返回值 void 返回值ModelAndView
    14. *
    15. * 正常情况下 按照普通类来对待即可
    16. * 没有继承 方法自定义 方法参数跟请求的key一致即可 参数普通类型 domain类型 map类型
    17. *
    18. * 配置(xml 注解)
    19. * 1.将这个Controller类的对象交个Spring当做Bean管理起来 @Controller
    20. * 2.请求和这个类及方法对应上才可以
    21. *
    22. * web-inf 文件 mvc-servlet.xml
    23. */
    24. @Controller
    25. public class TestController{
    26. @RequestMapping("test.do")
    27. public void test(String name){
    28. System.out.println("执行啦"+name);
    29. }
    30. }
    1. <?xml version="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. https://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. <!--<bean id="testController" class="controller.TestController"></bean>-->
    13. <context:component-scan base-package="controller"></context:component-scan>
    14. <!--启动一个Handler小弟 解析 类映射关系。。。。-->
    15. <!--<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">-->
    16. <!--<property name="mappings">-->
    17. <!--<props>-->
    18. <!--<prop key="test.do">testController</prop>-->
    19. <!--</props>-->
    20. <!--</property>-->
    21. <!--</bean>-->
    22. <!--先去做一个底层小弟Handler的加载-->
    23. <mvc:annotation-driven></mvc:annotation-driven>
    24. </beans>
    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Administrator
    4. Date: 2021/5/26
    5. Time: 22:09
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>$Title$</title>
    12. </head>
    13. <body>
    14. <a href="test.do?name=zzt">测试一下</a>
    15. </body>
    16. </html>