直接开始发车,使用工具idea
1.新建模块
2.配置web.xml文件
配置DispatcherServlet这个类服务启动时启动因为请求都要经过DispatcherServlet相当于中央处理器。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--在全局初始化参数中,把SpringMvc的配置文件加载进来-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc.xml</param-value>
</init-param>
<!--让服务启动起来的时候就创建DispatcherServlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--所有的请求都会经过dispatcherServlet-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
创建springmvc.xml配置文件
这个文件中主要配置InternalResourceViewResolver这个视图解析类查找解析视图(html,jsp)
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.yx.yxxit.mvc"/>
<!--配置视图解析器,就是controller转发页面的解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--所有的视图都在这个目录下找-->
<property name="prefix" value="/jsp/"/>
<!--配置文件的后缀名-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
写一个controller包用来存放请求类
package com.yx.yxxit.mvc.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
//请求的路径
@RequestMapping("mvc")
public class HelloMvc {
//请求路径
@RequestMapping("hello")
public String helloMvc() {
return "hello mvc";
}
}
启动tomcat服务器,请求路径localhost/springmvc/mvc/hello
这就已经搭建成功了
pom.xml文件引入
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yxxit</groupId>
<artifactId>springmvc-fx-002</artifactId>
<packaging>war</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
</project>