零基础学JavaWeb开发(二十二)之 springmvc入门到精通

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

一、SpringMVC概述

1、三层架构与MVC架构区别

1.1、三层架构

表示层主要对用户的请求接受以及数据的返回为客户端提供应用程序的访问。 servlet层

业务逻辑层对我们数据实现业务逻辑的封装 service层

数据访问层对数据库访问操作 dao层

com.mayikt.servlet----表示层

com.mayikt.service----业务逻辑层

com.mayikt.dao----数据库访问层

1.2、MVC架构

M 代表 模型Model

1.MVC全名是Model View Controller是模型(model)-视图(view)-控制器(controller)的缩写一种软件设计典范用一种业务逻辑、数据、界面显示分离的方法组织代码将业务逻辑聚集到一个部件里面在改进和个性化定制界面及用户交互的同时不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。

M 代表 模型Model业务逻辑层+数据库访问组合

模型就是数据就是 dao,bean

模型是应用程序中用于处理应用程序数据逻辑的部分。

通常模型对象负责在数据库中存取数据。

V 代表 视图View前端

视图就是网页, JSP用来展示模型中的数据

视图是应用程序中处理数据显示的部分。通常视图是依据模型数据创建的。

C 代表 控制器controller)

控制器的作用就是把不同的数据(Model)显示在不同的视图(View)上Servlet 扮演的就是这样的角色。

控制器是应用程序中处理用户交互的部分。通常控制器负责从视图读取数据控制用户输入并向模型发送数据。

1、三层是基于业务逻辑来分的而MVC是基于页面来分的

2、三层是软件架构通过接口实现编程MVC模式是一种复合设计模式一种解决方案

3、三层模式是体系结构模式MVC是设计模式

4、三层模式又可归于部署模式MVC可归于表示模式

1.3、前后端分离开发模式

体现 让专业的人做专业的事情,前端代码由前端来完成后端代码由我们后端来完成

后端程序只需要将接口数据提供给前端调用即可。

前端vue、饿了么UI、网页数据 例如 html、js、css

后端接口中数据 springmvc+mybatis

将前端和后端代码分开的

View视图层---jsp、ftl、js、css

com.mayikt.controller----控制层springmvc 底层基于servlet 控制页面跳转、控制页面展示数据

com.mayikt.controller----返回json 给前端

com.mayikt.service----业务逻辑层

com.mayikt.dao----数据库访问层

二、SpringMVC概述

1.SpringMVC 是一种基于 Java 实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,它和 Struts2 都属于表现层的框架属于 Spring FrameWork 的后续产品Spring MVC 分离了控制器、模型对象、过滤器以及处理程序对象的角色这种分离让它们更容易进行定制。

2.SpringMVC 已经成为目前最主流的 MVC 框架之一并且随着 Spring3.0 的发布全面超越 Struts2成 为最优秀的 MVC 框架它通过一套注解让一个简单的 Java 类成为处理请求的控制器而无须实现任何接口。同时它还支持 RESTful 编程风格的请求。

简单总结SpringMVC是一种基于Java实现MVC模型轻量级框架 底层基于Servlet封装。

三、SpringMVC环境搭建

1、SpringMVC环境搭建注解启动方式

创建maven工程

Maven依赖

     <!-- 整合springmvc框架依赖  -->
        <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>

创建控制器层

package com.mayikt.controller;

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

import java.util.HashMap;
import java.util.Map;

@Controller
public class MayiktController {
    /**
     * 1.@Controller作用
     * 需要在我们的类上加上@Controller注解 标记该类是为springmvc控制类
     * 2. @RequestMapping
     * 定义url映射
     * 3. @ResponseBody
     * 该接口返回我们的json数据
     */
//    /**
//     * 访问到该请求 返回json数据
//     *
//     * @return
//     */
    @RequestMapping("/getMayikt")
    @ResponseBody
    public Map<String, Object> getMayikt() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("msg", "ok");
        //访问该接口的 返回json数据
        return result;
    }
//    @RequestMapping("/getMayikt")
//    @ResponseBody
//    public String getMayikt() {
//        return "{code:'200',msg:'ok'}";
//    }

}

创建配置类

package com.mayikt.config;

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


@Configuration
@ComponentScan("com.mayikt.controller")
public class SpringMVCConfig {
    /**
     * 1.@Configuration  定义SpringMVCConfig.xml配置文件
     * 2.需要将我们的控制类注入到ioc容器 @ComponentScan("com.mayikt.controller")
     * @ComponentScan("com.mayikt.controller")将该包下所有的类 注入到IOC容器种
     * 3.在springmvc原理 所有请求过来先达到我们的 DispatcherServlet 分发具体控制类 方法执行
     */

}

注册配置类

package com.mayikt.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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;


public class ServletInitializer extends AbstractDispatcherServletInitializer {
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        // 注册我们的 springmvc config 配置类
        AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext
                = new AnnotationConfigWebApplicationContext();
        annotationConfigWebApplicationContext.register(SpringMVCConfig.class);
        return annotationConfigWebApplicationContext;
    }

    @Override
    protected String[] getServletMappings() {

        return new String[]{"/"};
    }

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

maven tomcat插件运行

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>85</port>
                    <path>/</path>
                    <ignorePackaging>true</ignorePackaging>
                </configuration>
            </plugin>
        </plugins>
    </build>

搭建环境常见问题

1.扫包范围填写错误 @ComponentScan("com.mayikt.controller")

2.在控制类没有加上@Controller

导致接口访问404

2、SpringMVC环境搭建xml启动方式

springboot 全是注解方式启动

Maven依赖

   <dependencies>
        <!-- 整合springmvc框架依赖  -->
        <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>
        
    </dependencies>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--1.配置spring创建容器时要扫描的包-->
    <context:component-scan base-package="com.mayikt.controller"></context:component-scan>
    <!--2.配置spring开启注解mvc 的支持-->
    <mvc:annotation-driven></mvc:annotation-driven>
    

</beans>

web.xml配置

所有请求过来都是先达到我们的DispatcherServlet

springmvc 基于 Servlet封装

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">


    <!-- SpringMVC前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

定义控制器

package com.mayikt.controller;

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

import java.util.HashMap;
import java.util.Map;


@Controller
public class MayiktController {
    /**
    * 1.@Controller作用
    * 需要在我们的类上加上@Controller注解 标记该类是为springmvc控制类
    * 2. @RequestMapping
    * 定义url映射
    * 3. @ResponseBody
    * 该接口返回我们的json数据
    */
    //    /**
    //     * 访问到该请求 返回json数据
    //     *
    //     * @return
    //     */
    
    @RequestMapping("/getMayikt")
    @ResponseBody
    public String getMayikt() {
        return "{code:'200',msg:'ok'}";
    }
    
}

外部tomcat运行

需要将这些jar包存入 tomcat lib目录 否则报错 找不到类

📎springmvc需要的依赖jar包.rar

📎apache-tomcat-8.5.78-windows-x64.zip

📎apache-tomcat-8.5.78-windows-x64.zip

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: SpringJava