1. Create project / module
New empty project:springMvcStudy
New module: type Maven webapp, name mvcStudy
2. Set Sources and Resources for module
Create two new folders under mvcStudy/src/main: java,resources
Open File/Project Structure/Project Settings/Modules, select mvcStudy, and click the Sources tab
Set the java folder to Sources and the Resources to Resources
3. Modify pom and add dependency:
1 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-core</artifactId> 5 <version>5.0.9.RELEASE</version> 6 </dependency> 7 <dependency> 8 <groupId>org.springframework</groupId> 9 <artifactId>spring-context</artifactId> 10 <version>5.0.9.RELEASE</version> 11 </dependency> 12 <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> 13 <dependency> 14 <groupId>org.springframework</groupId> 15 <artifactId>spring-beans</artifactId> 16 <version>5.0.9.RELEASE</version> 17 </dependency> 18 <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-expression</artifactId> 22 <version>5.0.9.RELEASE</version> 23 </dependency> 24 25 <dependency> 26 <groupId>org.springframework</groupId> 27 <artifactId>spring-web</artifactId> 28 <version>5.0.9.RELEASE</version> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-webmvc</artifactId> 33 <version>5.0.9.RELEASE</version> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-aop</artifactId> 38 <version>5.0.9.RELEASE</version> 39 </dependency>
4.Web.xml: add spring MVC configuration
1 <!--configure the setting of springmvcDispatcherServlet and configure the mapping--> 2 <servlet> 3 <servlet-name>springmvc</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:springmvc-config.xml</param-value> 8 </init-param> 9 <!-- <load-on-startup>1</load-on-startup> --> 10 </servlet> 11 12 <servlet-mapping> 13 <servlet-name>springmvc</servlet-name> 14 <url-pattern>/</url-pattern> 15 </servlet-mapping>
5. Create springmvc-config.xml in the resources file, as follows:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 11 "> 12 <!--Pay attention to configuration xsd,Or report a mistake:Wildcard matching is comprehensive, Element not found 'context:component-scan' Statement of.--> 13 14 <context:component-scan base-package="com.ice"/> 15 <!-- don't handle the static resource --> 16 <mvc:default-servlet-handler /> 17 <mvc:annotation-driven/> 18 19 <!-- configure the InternalResourceViewResolver --> 20 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 21 id="internalResourceViewResolver"> 22 <!-- prefix --> 23 <property name="prefix" value="/WEB-INF/jsp/" /> 24 <!-- Suffix --> 25 <property name="suffix" value=".jsp" /> 26 </bean> 27 28 </beans>
6. Create package, class
Base package = "com. Ice" defined above
Therefore, create package com.ice.controller; the test class is as follows:
1 package com.ice.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @RequestMapping("/") 7 @Controller 8 public class HomeController { 9 @RequestMapping("/") 10 public String index(){ 11 return "index"; 12 } 13 }
7. Confirm that there is an index.jsp file under WEB-INF/jsp
8. Release tomcat
Click Run - > run, select edit configure, click green '+', and add Tomcat server -- local,
a. Click the server tab: configure the application server as the local tomcat server
b. Click the deployment tab: click green '+', select artifact.., select the mode war expanded (automatically update to tomcat after modifying the file)
war mode: upload WEB project to the server in the form of package;
War expanded mode: upload the WEB project to the server with the location relationship of the current folder;
c. Click the server tab: VM options: the drop-down below can be changed to update resources
9. Operation
Click run to automatically access http://localhost:port/
Display the contents of index.jsp: hello world