SpringMVC环境搭配

概述

Spring MVC是Spring Framework提供的Web组件,全称是Spring Web MVC,是目前主流的实现MVC设计模式的框架,提供前端路由映射、视图解析等功能

mvc是什么

MVC是一种软件架构思想,把软件按照模型,视图,控制器来划分

Model:

模型层,指工程中的JavaBean,用来处理数据

JavaBean:

实体类Bean:专门用来存储业务数据,比如Student,User

业务处理Bean:指Servlet或Dao对象,专门用来处理业务逻辑和数据访问

View:

视图层,指工程中的html,jsp等页面,作用是和用户进行交互,展示数据

Controller:

控制层,指工程中的Servlet,作用是接收请求和响应浏览器

工作流程

1、 用户向服务端发送一次请求,这个请求会先到前端控制器DispatcherServlet(也叫中央控制器)。

2、DispatcherServlet接收到请求后会调用HandlerMapping处理器映射器。由此得知,该请求该由哪个Controller来处理(并未调用Controller,只是得知)

3、DispatcherServlet调用HandlerAdapter处理器适配器,告诉处理器适配器应该要去执行哪个Controller

4、HandlerAdapter处理器适配器去执行Controller并得到ModelAndView(数据和视图),并层层返回给DispatcherServlet

5、DispatcherServlet将ModelAndView交给ViewReslover视图解析器解析,然后返回真正的视图。

6、DispatcherServlet将模型数据填充到视图中

7、DispatcherServlet将结果响应给用户

导包

创建springmvc.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 自動扫描Spring的注解 -->
<context:component-scan base-package="com.xuexi.controller"></context:component-scan>
<!-- 扫描springmvc中的注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>

配置Web.xml

在web.xml中添加以下代码用于资源拦截

这里的*.cm后缀的cm可以自己更改,/*拦截所有资源包括图片

复制代码
<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.cm</url-pattern>
    </servlet-mapping>

Controller

这里出现404因为没用设置跳转页面,可以看到后台已经输出了

相关推荐
2021_fc4 分钟前
分布式应用可观测全链路追踪技术
java
数据的世界015 分钟前
JAVA和C#的语法对比
java·windows·c#
渡我白衣10 分钟前
深入理解 OverlayFS:用分层的方式重新组织 Linux 文件系统
android·java·linux·运维·服务器·开发语言·人工智能
百***926526 分钟前
java进阶1——JVM
java·开发语言·jvm
虫师c43 分钟前
字节码(Bytecode)深度解析:跨平台运行的魔法基石
java·jvm·java虚拟机·跨平台·字节码
q***72191 小时前
Spring Boot环境配置
java·spring boot·后端
洛_尘1 小时前
数据结构--7:排序(Sort)
java·数据结构
JIngJaneIL1 小时前
就业|高校就业|基于ssm+vue的高校就业信息系统的设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·高校就业
一 乐1 小时前
社区互助|社区交易|基于springboot+vue的社区互助交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·小区互助系统
q***57741 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot