转载自:https://www.cnblogs.com/xiaoliangyuu/p/15488138.html
==============
需要环境 jdk、maven、tomcat、idea,此次demo版本如下:
jdk 8
maven 3.5
tomcat 8.5
idea 2020.1.4
1、idea 点击 File->New ->Project,选择maven创建,并选择maven模板 maven-archetype-webapp(注意勾选Create from archetype,否则无法选择maven模板)
2、选择项目位置,填写maven三坐标GroupId/ArtifactId/Version
3、点击Next,预览如下图,点击Finish
4、打开pom.xml,添加spring依赖和springmvc依赖,并reload maven工程(idea可能会提醒,也可手动点击reload)如图:
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、新建文件夹
src/main/java
src/main/resources
src/main/webapp
src/main/webapp/WEB-INF
5.1、webapp即为web工程的目录文件,可自定义位置和名字,只要后面注意配置上对应目录就ok
5.2、标记java文件夹为 Sources Root,resources 文件夹为 Resources Root,方法如图:
建好好目录结构如图:
6、WEB-INF目录新建文件 web.xml
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、web目录下新建文件index.jsp
1 <html> 2 <body> 3 <h2>Hello World!</h2> 4 </body> 5 </html>
8、resources目录下新建文件 dispatcher-servlet.xml
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 <!--开启注解驱动--> 15 <mvc:annotation-driven/> 16 17 <!-- 定义视图解析器 --> 18 <bean id="velocityConfig" 19 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 20 <property name="prefix" value="/" /> 21 <property name="suffix" value="*.jsp" /> 22 </bean> 23 24 25 </beans>
9、src/main/java目录下新建包 com.yl.example,包下新建java文件 HelloController
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、建好文件后目录结构如图:
11、开始部署,点击右上角 Project Structure图标,弹出Project Structure 框。
12、依次点击Project、Modules、Artifacts标签核对如图:
原文地址:https://www.cnblogs.com/xiaoliangyuu/p/15488138.html
13、点击Modules -> + -> Web,如图
新建一个Web,如图
14、核对 Path 和 Web Resources Directory 图14,是否正确(Path指向web.xml,Resources Directory指向web工程目录)
我这里值如下:
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、点击最下面 create Artifact 按钮,生成 Artifact
16、无需配置,默认就好,生成Artifact效果如图。点击 Apply,OK。
17、配置tomcat,点击 Add Configuration -> + -> Tomcat Server ->Local (小技巧:点击+后可直接输入搜索)
18、点击 Configuration 配置为自己的Tomcat,配置Tomcat界面,截图如下:
19、我这里是 Tomcat8.5。注意勾选 After Lauch,这样后面tomcat启动成功后会自动打开浏览器并跳转到对应网址
20、点击 Depoyment -> + -> Artifact,添加我们刚刚配置的 Artifact
21、注意选择 spring-mvc-demo:war ,如图。点击Appy、OK。
22、点击运行Tomcat
23、启动tomcat成功后,target目录如下 (注意:lib文件夹下的jar包,有时候lib为空也可启动成功,与pom.xml文件的 <packaging>war</packaging> 有关)
24、启动成功,此时访问的是index.jsp的内容
25、访问 HelloController.java的内容
完整源码地址:
https://github.com/xiaoliangg/springboot-learning/tree/main/spring-mvc-demo
原文地址:https://www.cnblogs.com/xiaoliangyuu/p/15488138.html
标签:小亮,tomcat,web,springmvc,spring,idea,maven,点击,demo From: https://www.cnblogs.com/hd92/p/17072512.html