SpringMVC框架
一、SpringMVC概述
SpringMVC是隶属于Spring框架的一部分,主要是用来进行Web开发
,是对Servlet进行了封装。Servlet的主要作用:处理数据请求和响应。
SpringMVC是一种基于Java实现MVC模型的轻量级Web框架。
优点
- 使用简单、开发便捷(相比于Servlet)
- 灵活性强
二、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包引入
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>
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>