文章目录
步骤
1.新建Spring项目,添加web支持
2.Maven可能存在资源过滤的问题,需要完善配置
在pom.xml中添加以下配置:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
3.在pom.xml中引入相关依赖
Spring框架核心库、springmvc、servlet、JSTL等:
<!-- Servlet核心依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSTL标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- jsp -->
<!-- Servlet核心依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.29.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.29.RELEASE</version>
</dependency>
4.配置web.xml
1注册DispatcherServlet
2.关联SpringMVC的配置文件
3.启动级别为1
4.映射路径为 /
<?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">
<!-- 1、注册DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联一个springmvc的配置文件:【servlet-name】-servlet.xml -->
<init-param>
<param-name>contextConfiglocation</param-name>
<!-- classpath* 会到所有的包去找,不建议 -->
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动调用级别-1
与服务器一起启动
-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- / 匹配所有的请求:(不包括.jsp) -->
<!-- /* 匹配所有的请求:(包括.jsp) -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5.添加SpringMVC配置文件
springmvc-servlet.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
<context:component-scan base-package="com.demo.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp" />
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
6.创建Controller
HelloController.java
@Controller
@RequestMapping("/SpringMVC")
public class HelloController {
// localhost:8080/SpringMVC/hello
@RequestMapping("/hello")
public String hello(Model model){
// 封装数据
model.addAttribute("msg","Hello,SpringMVC!");
return "hello"; // 会被视图解析器处理
}
}
7.创建视图层
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
${msg}
</body>
</html>
8.配置Tomcat,开启服务,访问对应请求路径
学习视频链接:【狂神说Java】SpringMVC最新教程IDEA版通俗易懂
博主B站主页链接:遇见狂神说