Java学习笔记——SpringMvc(1) 介绍与快速入门


前言

本文是一名大学生学习java的笔记(基于黑马程序员的教程),主要为方便自己回顾和复习使用,同时也想督促自己坚持学习,也会把自己在学习中遇到的一些问题以及解决方法写出来供大家参考,欢迎友好交流。(第一次写博客不是很清楚,还请各位多包含)


一、SpringMvc是什么?

SpringMvc 隶属于Spring,是Spring技术中的一部分,是一种基于Java实现MVC模型的轻量级Web框架,它与Servlet技术功能等同,均属于web层开发技术。学习SpringMvc建议先学习JavaSE、JavaWeb、Maven、Spring基础


二、SpringMvc相较于Servlet的优点

众所周知框架是用来简化开发的。作为一种轻量级框架,SpringMVC与Servlet相比,开发起来更简单快捷,用更少的代码完成表现层代码的开发。简化的程度有多少呢?在这里插入图片描述
我们可以看到在itheima包的下面有servlet和springmvc两个包,后者明显少了好几个类,它们实现的功能却是相同的。而且当需要接收请求多个参数的时候,Servlet需要执行多次来接收参数,而SpringMvc就是通过形参接收的浏览器传递来的参数,所以相对的,程序越复杂,使用SpingMvc技术的效率也就越高


三、SpirngMvc工作流程

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

单次请求过程:
1.发送请求localhost/save
2.web容器发现所有请求都经过SpringMVC,将请求交给SpringMVC处理
3.解析请求路径/save
4.由/save匹配执行对应的方法save()
5.执行save()
6.检测到有@ResponseBody直接将save()方法的返回值作为响应求体返回给请求方


四、快速入门案例

1.pom.xml 配置文件

代码如下:

<?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.example</groupId>
  <artifactId>01_quickstart</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <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>

  <build>
    <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>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

    </plugins>


  </build>


</project>

2.package_config

代码如下:
springMvcConfig.class (bean配置)

package com.itcast.config;

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

//springmvc配置类,本质上还是一个spring配置类
@Configuration
@ComponentScan("com.itcast.controller")
public class SpringMvcConfig {
}

ServletContainersInitConfig.class (这部分是写死的,之后拿出来用即可)

package com.itcast.config;

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

//web容器配置类
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    //加载springmvc配置类,产生springmvc容器(本质还是spring容器)
    protected WebApplicationContext createServletApplicationContext() {
        //初始化WebApplicationContext对象
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        //加载指定配置类
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }

    //设置由springmvc控制器处理的请求映射路径
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //加载spring配置类
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

3.package_controller

UserController.class

package com.itcast.controller;

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

//定义表现层控制器bean
@Controller
public class UserController {

    //设置映射路径为/save,即外部访问路径
    @RequestMapping("/save")
    //设置当前操作返回结果为指定json数据(本质上是一个字符串信息)
    @ResponseBody
    public String save(){
        System.out.println("user save..");  //以此来模拟表示save方法成功执行,delete方法同理
        return "{'info':'springmvc'}";
    }

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

启动tomcat服务器后用Postman(或者直接在浏览器中输入 http://localhost/save ),会出现如下情况,即为运行成功,同时运行结果框里会打印一个user save…(delete方法同理)
在这里插入图片描述


五、遇到的问题(子容器启动失败)

此问题参考博客: tomcat服务器出现【A child container failed during start】问题解决

tomcat服务器启动时报错: A child container failed during start(子容器启动失败)
本人参考的解决办法:
在这里插入图片描述

总结

作为当前市面上主流的web层开发技术,SpringMvc既方便效率也很高,很值得我们去学习。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值