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因为没用设置跳转页面,可以看到后台已经输出了

相关推荐
折哥的程序人生 · 物流技术专研3 分钟前
《Java面试85题图解版(三)》上篇:高阶架构设计篇
java·开发语言·后端·面试·职场和发展
吴声子夜歌10 分钟前
Java——ArrayDeque
java·arraydeque
NagatoYukee16 分钟前
Spring/SpringMVC/SprongBoot知识复习
java·数据库·spring
泓博27 分钟前
docker ubuntu源码安装openclaw的常见问题
java·linux·开发语言·ai
YuanDaima204828 分钟前
WSL2 核心中间件部署实战:MySQL、Redis 与 RocketMQ
java·数据库·人工智能·redis·python·mysql·rocketmq
南境十里·墨染春水29 分钟前
线程池学习(一) 理解 进程 线程 协程及上下文切换
java·开发语言·学习
知兀31 分钟前
@Accessors(chain = true)和@Builder链式风格差异
java·开发语言
i220818 Faiz Ul32 分钟前
个人健康系统|健康管理|基于java+Android+微信小程序的个人健康系统设计与实现(源码+数据库+文档)
android·java·vue.js·spring boot·微信小程序·毕设·个人健康系统
组合缺一36 分钟前
agentscope-harness vs solon-ai-harness:Java 智能体「马具引擎」的双雄对决
java·人工智能·ai·llm·agent·solon·agentscope
Javatutouhouduan9 小时前
2026Java面试的正确打开方式!
java·高并发·java面试·java面试题·后端开发·java编程·java八股文