Build Maven + spring MVC + Tomcat demo project with idea

The environment jdk, maven, tomcat and idea are required. The demo version is as follows: ...

The environment jdk, maven, tomcat and idea are required. The demo version is as follows:

jdk 8
maven 3.5
tomcat 8.5
idea 2020.1.4

1. idea click file - > New - > project, select maven create, and select maven archetype webapp (note that check Create from archetype, otherwise you cannot select maven template)

2. Select the project location and fill in the maven triad GroupId/ArtifactId/Version

3. Click Next to preview as shown in the figure below, and click Finish

4. Open pom.xml, add spring dependency and spring MVC dependency, and reload maven project (idea may remind you, or click reload manually), as shown in the figure:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.yl.example</groupId> 8 <artifactId>spring-mvc-demo</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>spring-mvc-demo Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 22 23 <dependencies> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-context</artifactId> 27 <version>5.2.7.RELEASE</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-webmvc</artifactId> 32 <version>5.2.7.RELEASE</version> 33 </dependency> 34 </dependencies> 35 </project>

5. New folder

src/main/java
src/main/resources
src/main/webapp
src/main/webapp/WEB-INF
5.1 webapp is the directory file of the web project. You can customize the location and name. As long as you pay attention to the corresponding directory on the configuration later, it's ok
5.2 mark the java folder as Sources Root and the Resources folder as Resources Root, as shown in the figure:

  The directory structure is shown in the figure below:

6. Create a new file web.xml in the WEB-INF directory

1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 8 9 <servlet> 10 <servlet-name>springmvc</servlet-name> 11 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 12 <init-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:dispatcher-servlet.xml</param-value> 15 </init-param> 16 </servlet> 17 18 <servlet-mapping> 19 <servlet-name>springmvc</servlet-name> 20 <url-pattern>/</url-pattern> 21 </servlet-mapping> 22 </web-app>

7. Create a new file index.jsp in the web directory

1 <html> 2 <body> 3 <h2>Hello World!</h2> 4 </body> 5 </html>

8. Create a new file dispatcher-servlet.xml in the resources directory

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 xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 11 12 <context:component-scan base-package="com.yl.example"/> 13 14 <!--Enable annotation driven--> 15 <mvc:annotation-driven/> 16 17 <!-- Define view parser --> 18 <bean id="velocityConfig" 19 > 20 <property name="prefix" value="/" /> 21 <property name="suffix" value="*.jsp" /> 22 </bean> 23 24 25 </beans>

9. Create a new package com.yl.example under src/main/java directory, and create a new java file HelloController under the package

1 package com.yl.example; 2 3 import org.springframework.web.bind.annotation.GetMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 /** 7 * @description: 8 * @author: xiaoliang 9 * @date: 2021/10/30 23:08 10 **/ 11 @RestController 12 public class HelloController { 13 14 15 @GetMapping("/test") 16 public String test(){ 17 return "hello spring-mvc"; 18 } 19 20 21 }

10. After the file is built, the directory structure is shown in the figure below:

  11. Start deployment, click the Project Structure icon in the upper right corner to pop up the Project Structure box.

12. Click the Project, Modules and Artifacts tabs to check, as shown in the figure:

13. Click modules - > + - > Web, as shown in the figure

Create a new Web, as shown in the figure

14. Check whether Path and Web Resources Directory Figure 14 are correct (Path points to web.xml and resources directory points to web project directory)

My values here are as follows:

Path:F:\01_idea_workspace\my_workspace\springboot-learning\spring-mvc-demo\src\main\webapp\WEB-INF\web.xml
Resources Directory:F:\01_idea_workspace\my_workspace\springboot-learning\spring-mvc-demo\src\main\webapp

15. Click the create Artifact button at the bottom to generate an Artifact

  16. No configuration is required. It's good by default. The Artifact effect is generated, as shown in the figure. Click Apply and OK.

17. To configure tomcat, click Add configuration - > Tomcat server - > local (tip: click + to enter search directly)

18. Click Configure as your own Tomcat to configure the Tomcat interface. The screenshot is as follows:

19. This is tomcat 8.5. Note that check after launch, so that the browser will automatically open and jump to the corresponding website after tomcat is successfully started

  20. Click deployment - > + - > Artifact to add the Artifact we just configured

21. Pay attention to selecting spring MVC Demo: War, as shown in the figure. Click Appy and OK.

  22. Click to run Tomcat

  23. After starting tomcat successfully, the target directory is as follows (Note: sometimes the jar package under lib folder can be started successfully if lib is empty, which is related to < packaging > war < / packaging > in pom.xml file)

24. The startup is successful. At this time, the content of index.jsp is accessed

25. Access the contents of HelloController.java

Full source address:

https://github.com/xiaoliangg/springboot-learning/tree/main/spring-mvc-demo

30 October 2021, 13:18 | Views: 3511

Add new comment

For adding a comment, please log in
or create account

0 comments