🎊专栏【SpringMVC】
🍔喜欢的诗句:天行健,君子以自强不息。
🎆音乐分享【如愿】
🎄欢迎并且感谢大家指出小吉的问题🥰
文章目录
- 🎍SpringMVC简介
 - ⭐优点
 
- 🌺SpringMVC入门案例
 - ⭐案例用到的注解
 - 🎆延申
 
- 🌺入门案例流程分析
 - 🍔简化入门案例
 

 Spring MVC是Spring框架中的一员,是目前最主流的Java EE Web框架之一。在企业级开发中,Spring MVC有非常广泛的应用。
Spring MVC基于MVC设计模式,将web层进行职责解耦,基于请求映射将请求分派到指定的控制器进行处理。它主要特点有:
使用DispatcherServlet作为前端控制器,将请求调度到控制器
 支持灵活的请求映射方式,方便接口对接
 提供强大的数据绑定和验证机制
 支持多种视图技术,可以方便与不同UI框架集成
 拥有强大的扩展点,可以自定义很多组件
 同时,Spring MVC作为Spring框架的一部分,可以非常容易地集成Spring的其他模块,如Spring IoC、Spring JDBC等。这为企业级应用提供了一种非常灵活而强大的web框架。
以上补充了Spring MVC的定位、主要特点等内容,使开头部分更丰富充实,希望这些补充内容确实能够帮助读者更好地了解Spring MVC
通过跟随本文,你可以学习创建SpringMVC项目,编写Controller处理请求,配置SpringMVC加载bean,并运行调试一个简单的案例。这将让你对SpringMVC有最直观的感受,打下坚实的基础。跟我一起开始SpringMVC之旅吧!
🎍SpringMVC简介
Spring MVC是一种基于Java开发的Web应用程序框架,它是Spring Framework的一部分。它提供了一种模型-视图-控制器(Model-View-Controller,MVC)的架构模式,用于构建灵活、可维护和可扩展的Web应用程序。
⭐优点
- 使用简单,开发便捷(相对于Servlet)
 - 灵活性强
 
🌺SpringMVC入门案例
创建项目

在pom.xml文件中导入包和springmvc坐标,servlet坐标
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
 

添加tomcat插件
<plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>80</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
 

创建UserController类
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//使用@Controller,定义bean
@Controller
public class UserController {
    //设置当前操作的访问路径
    @RequestMapping("/save")
    //设置当前操作的返回值类型
    @ResponseBody  //@ResponseBody把你返回的东西整体作为响应的内容给到外面
    public String save(){
        System.out.println("user save ...");
    //返回一个json数据
        return "{'module':'springmvc'}";
    }
}
 

创建新的包,来配置SpringMVC
创建SpringMVC的配置文件,加载controller对应的bean
package com.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//配置类
@Configuration
//扫描
@ComponentScan("com.example.controller")
public class SpringMvcConfig {
    
}
 
定义一个servlet容器启动的配置类,在里面加载spring的配置

 
package com.example.config;
//定义一个servlet容器启动的配置类,在里面加载spring的配置
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
public class ServletConfig extends AbstractDispatcherServletInitializer {
    //加载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() {
        return null;
    }
}
 

启动程序


 
运行localhost/save,运行成功
 
 
⭐案例用到的注解

 
 
🎆延申
我们在UserController类中加上这一段代码
 保持原来的代码不变
 @RequestMapping("delete")
    @ResponseBody
    public String delete(){
        System.out.println("user delete ...");
        return "{'module':'springmvc delete'}";
    }
 

这次我们不运行
localhost/save了,我们运行localhost/delete
运行成功

 所以以后我们配置好一个springmvc案例后,再开发功能时,不用重新新建,直接在原项目里面写入即可
🌺入门案例流程分析

🍔简化入门案例
我们修改ServletConfig里面的代码
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
 

运行,成功
 
通过这个简单的入门案例,我们已经体验了Spring MVC的基本用法,构建了一个最小可运行的Spring MVC应用。但要真正使用Spring MVC进行企业级Web开发,我们还需要学习许多更高级的功能。
例如Spring MVC强大的视图技术,可以轻松集成各种模版引擎,而不仅仅是返回字符串。数据验证功能可以保证数据合法性。文件上传和下载更方便处理文件内容。拦截器功能可以实现日志、权限等通用处理。还有国际化、主题切换、RESTful服务等等。
Spring MVC构建的应用可以轻松应对复杂的企业级需求。它的扩展点设计更让我们可以自定义各种组件。一定要继续深入学习Spring MVC,以应对工作中的各种挑战。
本文只是入门第一步。如果大家在学习和使用Spring MVC过程中,遇到任何问题,请积极到评论区讨论。
 























