SpringMVC简述与快速入门案例

简述

  • SpringMVC技术与Servlet功能相同,均属于web层开发技术
  • SpringMVC是一种基于Java实现MVC模型的轻量级Web框架

复习Servlet开发

@WebServlet("/user/save")
public class UserSaveServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.请求参数
        String name = request.getParameter("name");
        System.out.println("servlet save name ==>" + name);
        //2.产生影响
        response.setContentType("text/json;charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.write("{'module':'servlet save'}");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

在这里插入图片描述

  • 注意修改应用程序上下文

快速入门案例

  • 创建项目:

在这里插入图片描述

①使用SpringMVC技术需要先导入SpringMVC坐标与Servlet坐标

  • 先清除事先存在的东西
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.itheima</groupId>
    <artifactId>spring_01_quickstart</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>


</project>
  • 然后自己导入坐标,注意这里版本号不能乱改,不然会有版本冲突不能运行!
<dependencies>
    <!--        导入坐标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>

</dependencies>
  • 可以看到spring-webmvc已经将之前学习的包都有所导入,所以不用导入那些包

在这里插入图片描述

②创建SpringMVC控制器类(等同于Servlet功能)

//2.定义Controller
//2.1使用@Controller定义bean
@Controller
public class UserController {

    //2.2设置当前操作的访问路径
    @RequestMapping("/save")
    //2.3设置当前操作的返回值类型,相当于将函数类型的返回值作为一个整体返回
    @ResponseBody
    public String save() {
        System.out.println("user save ... ");
        return "{'info':'springmvc'}";
//        return "Hello";
    }

    //设置映射路径为/delete,即外部访问路径
    @RequestMapping("/delete")
    @ResponseBody
    public String delete() {
        System.out.println("user delete ...");
        return "{'info':'springmvc'}";
    }
}

③初始化SpringMVC环境(同Spring环境),设定SpringMVC加载对应的bean

//3.创建springmvc配置文件,加载controller对应的bean
@Configuration
@ComponentScan("com.itheima.controller")
public class SpringMvcConfig {

}

④初始化Servlet容器,加载SpringMVC环境,并设置SpringMVC技术处理的请求

//4.定义一个servlet容器启动的配置类,在里面加载spring配置,要继承一个接口,实现三个方法
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
    //    加载springMVC容器配置


    @Override
    protected WebApplicationContext createServletApplicationContext() {
//        AnnotationConfigApplicationContext ctx =
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);
        return ctx;

//        return null;
    }

    //设置哪些请求归属于SpringMVC处理
    @Override
    protected String[] getServletMappings() {
        //表示所有请求交给SpringMVC处理
        return new String[]{"/"};
    }

    //加载Spring容器配置
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}
  • pom插件补上
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>80</port>
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>
  • 添加运行配置,添加运行:tomcat7:run以及修改工作目录

在这里插入图片描述

  • 删除WEB_INF下的文件

相关知识点复习

  • 名称:@Controller
  • 类型:类注解
  • 位置:SpringMVC控制器类定义上方
  • 作用:设定SpringMVC的核心控制器bean
  • 范例
@Controller
public class UserController{
    
}
  • 名称:@ResponseBody
  • 类型:方法注解
  • 位置:SpringMVC控制器方法定义上方
  • 作用:设置当前控制器方法相应内容为当前返回值,无需解析
  • 范例:
@RequestMapping("/save")
@ResponseBody
public String save(){
    System.out.println("user save ... ");
    return "{'info':'springmvc'}";
}

入门案例工作流程

  • 启动服务器初始化过程
    • 1.服务器启动,执行ServletContainerInitConfig类,初始化web容器
    • 2.执行createServletApplicationContext方法,创建了WebApplicationContext对象
    • 3.加载SpringMvcConfig
    • 4.执行@ComponentScan加载对应的bean
    • 5.加载UserController,每个@RequestMapping的名称对应一个具体的方法
    • 6.执行getServletMapping方法,定义所有的请求都通过SpringMVC

SpringMVC-bean加载控制

  • 名称:@ComponentScan
  • 类型:类注解
  • 范例:
@Configuration
//1.指定特殊包
//@ComponentScan({"com.itheima.service","com.itheima.dao"})
//2.全局扫描,排除部分
//表示按照注解排除Controller
@ComponentScan(value = "com.itheima"
        , excludeFilters = @ComponentScan.Filter(
                type = FilterType.ANNOTATION,
                classes = Controller.class
        )
        )
public class SpringConfig {

}
  • 属性
    • excludeFilters:排除扫描路径中加载的bean,需要指定到类别(type)与具体项(classess)
    • includeFilters:加载指定的bean,需要指定到类别(type)与具体项(classess)

Controller加载控制与业务bean加载控制

在这里插入图片描述

  • 简化开发,实现最后一个接口就行
public class ServletContainerInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //    //    加载springMVC容器配置
//    @Override
//    protected WebApplicationContext createServletApplicationContext() {
//        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
//        ctx.register(SpringMvcConfig.class);
//        return ctx;
//    }
//
//    //设置哪些请求归属于SpringMVC处理
//    @Override
//    protected String[] getServletMappings() {
//        //表示所有请求交给SpringMVC处理
//        return new String[]{"/"};
//    }
//
//    //加载Spring容器配置
//    @Override
//    protected WebApplicationContext createRootApplicationContext() {
//        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
//        ctx.register(SpringConfig.class);
//        return ctx;
//
//    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值