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

相关推荐

  1. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-01-21 12:14:02      62 阅读
  2. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-01-21 12:14:02      53 阅读
  3. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-01-21 12:14:02      53 阅读
  4. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-01-21 12:14:02      60 阅读
  5. <span style='color:red;'>springMVC</span>

    springMVC

    2024-01-21 12:14:02      46 阅读
  6. <span style='color:red;'>SpringMVC</span>

    SpringMVC

    2024-01-21 12:14:02      38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-21 12:14:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-21 12:14:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-21 12:14:02       82 阅读
  4. Python语言-面向对象

    2024-01-21 12:14:02       91 阅读

热门阅读

  1. SpringBoot开启 Actuator springboot开启actuator监控信息

    2024-01-21 12:14:02       60 阅读
  2. 面试问题记录【深圳,共三面,A 轮公司】

    2024-01-21 12:14:02       46 阅读
  3. leetcode上两个字符串之间的动态规划类题目

    2024-01-21 12:14:02       47 阅读
  4. 魔域新端辅助背包遍历

    2024-01-21 12:14:02       43 阅读