随笔 - 217  文章 - 4  评论 - 4  阅读 - 23587

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   跨越&尘世  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示