零基础学springMVC-01

springMVC
  1. 什么是springMVC?

    它是基于MVC开发模式的框架,用来优化控制器,它是spring家族的一员,它具备IOC和AOP

  2. 什么是MVC

    它是一种开发模式,它是模型视图控制器的简称,所有的web应用都是基于MVC开发

    M: 模型层 包含实体类,业务逻辑层,数据访问层

    V:视图层 html,javascript,vue 等都是视图层,用来显示数据

    C:控制器 它是用来接收客户的请求,并且返回响应到客户端的组件,servlet就是组件

  3. 示意图

     

  4. springMVC的优点

    1. 轻量级,基于MVC的框架

    2. 易于上手,容易理解,功能强大

    3. 它具备IOC和AOP

    4. 完全基于注解开发

  5. 时序图

     

  6. springMVC的执行流程

     

  7. 基于注解的springmvc的框架开发步骤

    1. 新建项目,选择webapp模板

    2. 修改目录,添加缺失的目录并修改属性

    3. 修改pom.xml文件,添加springmvc的依赖,添加servlet依赖

    4. 添加springmvc.xml文件,指定包扫描,添加视图解析器

    5. 新建web.xml并且注册soringmvc框架(所有的请求都是来自srvlet)

    6. 新建index.jsp页面,发送请求给服务器

    7. 开发控制器(srvlet)它是一个普通类

    8. 添加tomcat进行功能测试

  8. web请求分析

    核心处理器

    index.jsp <------> dispatcherServlet <------->springmvc的处理器是一个普通类

    one.jsp<---------->dispatcherServlet <------->springmvc的处理器是一个普通类

    dispatcherServlet 要在web.xml文件中注册才可以使用

  9. 注册springmvc框架

  10. 开发index.jsp

  11. @RequestMappering 注解详解

    此注解可加在方法上,是为此方法注册一个可以访问的名称

    此注解可以加在类上,相当于是包名(虚拟路径)

  12. @RequestMappering 可以区分get 和post 请求

  13. springmvc.xml

     <?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"
            xsi:schemaLocation="
             http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.2.xsd">
     <!--    添加包扫描-->
             <context:component-scan base-package="com.lgy.controller">
     ​
             </context:component-scan>
     <!--    添加视图解析器-->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <!--        设置前缀-->
             <property name="prefix" value="/admin/"></property>
     <!--       设置后缀 -->
             <property name="suffix" value=".jsp"></property>
         </bean>
     ​
     </beans>
     ​
  14. web.xml

     <?xml version="1.0" encoding="UTF-8"?>
     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
              version="4.0">
     <!--    注册框架-->
         <servlet>
             <servlet-name>springmvc</servlet-name>
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <init-param>
                 <param-name>contextConfigLocation</param-name>
                 <param-value>classpath:springmvc.xml</param-value>
             </init-param>
         </servlet>
         <servlet-mapping>
             <servlet-name>springmvc</servlet-name>
     <!--        拦截请求
                指定什么样的请求
                .action 请求才会被拦截
     -->
             <url-pattern>*.action</url-pattern>
         </servlet-mapping>
     ​
     ​
     </web-app>
  15. DemoAction.java

     package com.lgy.controller;
     ​
     import org.springframework.stereotype.Controller;
     import org.springframework.web.bind.annotation.RequestMapping;
     ​
     @Controller
     @RequestMapping("/user")
     public class DemoAction {
         @RequestMapping("/demo")
         public String demo(){
             System.out.println("服务器被访问.....");
             return "main";
         }
     }
     ​
  16. 配置tomcat测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值