SpringMVC学习01

springmvc框架的使用,使用注解进行开发,与mybatis结合使用,可以替代servlet

使用springmvc框架首先导入坐标

<!--1.导入坐标springmvc与servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>

加载springmvcConfig bean类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.itheima.controller") 定义需要扫描的包
public class SpringMvcConfig {
}

初始化Config

package com.itheima.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

//定义servlet容器启动的配置类,加载springmvc配置
public class InitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    //     ·启动服务器初始化过程
// 1.服务器启动,执行ServletContainersInitConfig类,初始化web容器
// 2.执行createServletApplicationContext方法,创建了WebApplicationContext对象
// 3.加载SpringMvcConfig
// 4.执行@ComponentScan加载对应的bean
// 5,加载UserController,每个@RequestMapping的名称对应一个具体的方法
// 6,执行getServletMappings方法,定义所有的请求都通过SpringMVC

//     。单次请求过程
// 1.发送请求1 ocalhost/save
// 2.web容器发现所有请求都经过SpringMVC,将请求交给SpringMVC处理
// 3,解析请求路径/save
// 4.由/save匹配执行对应的方法save()
// 5.执行save()
// 6,检测到有@ResponseBody直接将save()方法的返回值作为响应求体返回给请求方
//加载springmvc容器配置
    @Override
     protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
         ctx.register(SpringMvcConfig.class);
        return ctx;
     }
     //设置那些请求归属springmvc处理
     @Override
     protected String[] getServletMappings() {
         return new String[]{"/"};
     }
     //加载spring容器配置
    @Override
     protected WebApplicationContext createRootApplicationContext() {
         AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
         ctx.register(SpringConfig.class);
         return ctx;
     }
} 

使用Controller

package com.itheima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller //定义bean
public class UserController {

    //设置当前访问路径
    @RequestMapping("/save")
    //设置当前操作的返回值类型
    @ResponseBody
    public String save(){
        System.out.println("user save ...");
        return "{'module':'springmvc'}";
    }

    @RequestMapping("/delete")
    //设置当前操作的返回值类型
    @ResponseBody
    public String delete(){
        System.out.println("user delete ...");
        return "{'module':'springmvc_del'}";
    }
}

 

posted on 2022-10-20 22:17  跨越&尘世  阅读(19)  评论(0编辑  收藏  举报