SpringMVC的正式名称是Spring Web MVC
- 是属于Spring框架的一部分
- 是基于Servlet API的Web框架
SpringMVC的核心功能是:拦截和处理客户端的请求
一、pom.xml配置
<!--JavaEE基本依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<!--SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
二、控制器配置
package com.mj.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@RequestMapping("/addUser")
@ResponseBody
public String add() {
return "Add Success";
}
}
三、maven创建web项目:
1、web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置SpringMVC自带的DispatchCherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--Spring的配置文件位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!--项目一旦部署到服务器,就会创建servlet ,值越小越先创建-->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--拦截所有请求-->
<!-- *.do 只会拦截.do结尾的资源 不会拦截动态资源(比如*.jsp)、静态资源(比如*.html、*.js) -->
<!-- / 会拦截静态资源(比如*.html、*.js)不会拦截动态资源(比如*.jsp) -->
<!-- /* 会拦截静态资源(比如*.html、*.js)会拦截动态资源(比如*.jsp)-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--为了解决POST请求乱码问题-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2、index.jsp配置如下:
<%--
Created by IntelliJ IDEA.
User: majiancheng
Date: 2022/10/9
Time: 上午9:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
This is index.jsp
<form action="addUser" method="post">
<div>
用户名<input name="username">
</div>
<div>
密码<input name="password">
</div>
<div>
<button type="submit">添加</button>
</div>
</form>
</body>
</html>
3、applicationContext.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:contexg="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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<contexg:component-scan base-package="com.mj"/>
<!-- <mvc:annotation-driven />保证@Controller能够正常使用-->
<!-- <mvc:annotation-driven/>-->
<!--返回数据不乱码-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="defaultCharset" value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--DispatcherServlet不想处理的请求交回给Tomcat的默认DefaultServlet去处理-->
<mvc:default-servlet-handler />
</beans>
项目结构如下: