在编译的spring源码中,新建个测试web项目。

配置项目

新建 module,选择 gradle web 项目,填写项目信息后完成。
新建 Springweb 测试项目 - 图1

修改构建文件 build.gradle

  • 增加项目依赖
  • 增加servlet依赖即可 ```properties plugins { id ‘war’ id ‘java’ }

group ‘org.springframework’ version ‘5.2.22.RELEASE’

repositories { mavenCentral() mavenCentral() }

dependencies { compile(project(“:spring-context”)) compile(project(“:spring-web”)) compile(project(“:spring-webmvc”)) providedCompile group: ‘javax.servlet’, name: ‘javax.servlet-api’, version: ‘3.1.0’ providedCompile group: ‘javax.servlet’, name: ‘jsp-api’, version: ‘2.0’ testImplementation ‘junit:junit:4.11’ testImplementation ‘org.junit.jupiter:junit-jupiter-api:5.8.1’ testRuntimeOnly ‘org.junit.jupiter:junit-jupiter-engine:5.8.1’ }

test { useJUnitPlatform() }

  1. 因为是spring web项目,所以需要mvc的配置文件,applicationContext.xml。在 resouces 文件夹下新建配置文件:
  2. ```xml
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns="http://www.springframework.org/schema/beans"
  8. xsi:schemaLocation="http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  12. http://www.springframework.org/schema/beans
  13. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
  14. <context:component-scan base-package="cn.lichenghao"/>
  15. <mvc:annotation-driven/>
  16. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  17. <property name="prefix" value="/WEB-INF/views/"/>
  18. <property name="suffix" value=".jsp"/>
  19. </bean>
  20. <mvc:default-servlet-handler/>
  21. </beans>

web.xml。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:applicationContext.xml</param-value>
  9. </context-param>
  10. <listener>
  11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  12. </listener>
  13. <servlet>
  14. <servlet-name>dispatcherServlet</servlet-name>
  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16. <init-param>
  17. <param-name>contextConfigLocation</param-name>
  18. <param-value>classpath:applicationContext.xml</param-value>
  19. </init-param>
  20. <load-on-startup>1</load-on-startup>
  21. <async-supported>true</async-supported>
  22. </servlet>
  23. <servlet-mapping>
  24. <servlet-name>dispatcherServlet</servlet-name>
  25. <url-pattern>/</url-pattern>
  26. </servlet-mapping>
  27. </web-app>

按照配置文件的包路径新建测试类。

  1. package cn.lichenghao.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. public class HomeController {
  6. @RequestMapping("/")
  7. public String home() {
  8. return "index";
  9. }
  10. }

配置完毕后项目结构:
新建 Springweb 测试项目 - 图2

部署运行

部署在tomcat下运行,新增 Run Configurations
新建 Springweb 测试项目 - 图3

Deployment 选择我们的项目即可,运行访问即可。
新建 Springweb 测试项目 - 图4