一、创建一个普通的Maven项目
1、文件-新建-项目
2、填写相关名称以及相应位置
3、设置Maven路径
二、编写程序
1、配置pom依赖
<properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- 日志 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- ServletAPI --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
2、创建web.xml
在webapp-WEB-INF下创建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"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!--拦截匹配规则的url请求,进入springmvc框架处理--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3、创建springmvc.xml
在src-main-resources下创建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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--开启controller扫描--> <context:component-scan base-package="com.wangsiqi.controller"/> <!--配置springmvc的视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 1、声明处理器映射器,目的:选择哪一个处理期来处理当前对象--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!--2、声明处理器适配器 目的:调用胡处理器的处理请求的方法--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!--配置视图解析器,目的:处理视图信息--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 声明处理器--> <bean name="/helloworld" class="com.wangsiqi.controller.TestController"></bean> <!-- 自动注册最合适的处理器映射器,处理器适配器(调用handler方法) --> <!-- <mvc:annotation-driven/>--> <!-- 配置spring开启注解mvc的支持 --> <mvc:annotation-driven></mvc:annotation-driven> </beans>
4、创建TestController类
在src-main-java-com.controller下创建TestController类
@RestController @RequestMapping("/test") public class TestController { @GetMapping("/hello") public String hello(){ return "hello"; } }
三、配置Tomcat
1、文件(Feild)-项目结构(Project structure)-工件(Artifacts)-Web应用程序:展开型(Web Aplication:Exploded)-基于模块(From Modules)-选择需要配置的模块名称,然后点击Facet-Web下的模块,进行路径配置
2、点击项目右上角编辑配置(Edit Configurations)-左上角±Tomcat服务器(Tomcat Server)-本地(localhost)
3、设置路径
4、设置部署(Deployment)-工件(Artifact),需注意应用程序上下文(Application context)的路径和服务器(Server)中的URL一致
四、项目启动