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

相关推荐
Raas1005 小时前
AI网关如何接入?MAI Gateway部署实操教程
java·开发语言·人工智能·gateway·ai网关·mai gateway·企业级产品
前端双越老师5 小时前
程序员 1v1 访谈系列第十一期:应届生别做开发
java·agent·ai编程
Captain1314_李祖团5 小时前
5. MLIR相关语法
java·前端·javascript
Rain的Java大神之路5 小时前
面试官让你把 LLM、RAG、Agent、MCP 串起来讲清楚,90% 的 Java 工程师当场露馅
java·后端·面试
无所谓^_^5 小时前
个人健康管理系统 | Springboot3 | Vue3
java·vue·毕业设计·springboot·课程设计·毕设·前后端分离
杨凯凡5 小时前
Spring Boot 的 @Transactional 为什么会失效?从代理、自调用到异常回滚的排查实战
java·spring boot·spring·事务·后端开发
曼岛_6 小时前
[图解密码学]第5章 对称加密基础
java·数据库·密码学
哦虎!6 小时前
【JavaEE进阶】SpringBoot
spring
天河归来16 小时前
第三方接口不稳定,如何让核心任务继续完成
java·大数据
Knight_AL16 小时前
Java 文件下载时文件名中的 `#` 被转换成 `%23`?一次 URL 编码与解码问题排查
java·开发语言