SpringMVC框架

SpringMVC框架

一、SpringMVC概述

SpringMVC是隶属于Spring框架的一部分,主要是用来进行Web开发,是对Servlet进行了封装。Servlet的主要作用:处理数据请求和响应。

SpringMVC是一种基于Java实现MVC模型的轻量级Web框架。

优点

  • 使用简单、开发便捷(相比于Servlet)
  • 灵活性强

image-20230329110740472

二、SpringMVC环境搭建

1、项目准备

1.1 创建动态Web项目 springmvc-T1

配置需要使用 web.xml ,在创建项目时将生成 web.xml 勾上

1.2 导入SpringMVC资源

  • Maven引入

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
    </dependencies>
    
  • jar包引入

    image-20230329112521181

2、SpringMVC核心配置文件

创建SpringMVC的核心配置文件 springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    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/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
	
	<!-- 开启注解包扫描 -->
	<context:component-scan base-package="com.ynnz"></context:component-scan>
	
	<!-- 配置springmvc注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven>	
</beans>

image-20230329135654552

3、SpringMVC前端控制器

web.xml中配置SpringMVC前端控制器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
	<display-name>springmvc-T1</display-name>

	<!-- 前端控制器-->
	<servlet>
		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值