1.建立project / module
新建空的project:springMvcStudy 新建module:type maven-webapp,名字mvcStudy
2.为module设置Sources和Resources
在mvcStudy/src/main下新建2个文件夹:java,resources.打开File/Project Structure/Project Settings/Modules 选择mvcStudy,点击Sources选项卡设置java文件夹为Sources,设置resources为Resources.
3.修改pom,增加依赖:
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>
View Code
4.Web.xml:增加SpringMVC的配置
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.在resources文件下建立 springmvc-config.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 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 <!--注意配置xsd,要么报错:通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。-->
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 <!-- 前缀 -->
23 <property name="prefix" value="/WEB-INF/jsp/" />
24 <!-- 后缀 -->
25 <property name="suffix" value=".jsp" />
26 </bean>
27
28 </beans>
6.创建包,类
上面定义的扫描包 base-package="com.ice".因此,建立 package com.ice.controller; 测试类如下:
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.确认 WEB-INF/jsp下有index.jsp文件.
8.发布tomcat
点击run->run,选择edit configure,点击绿色'+',增加tomcat server |--local,a.点击server选项卡:配置application server为本地tomcat server.b.点击deployment选项卡:点击绿色'+',选择artifact..,选择模式 war exploded模式 (修改文件后自动更新到tomcat.)war模式:将WEB工程以包的形式上传到服务器 ;war exploded模式:将WEB工程以当前文件夹的位置关系上传到服务器;c.点击server选项卡:VM options:下边的下拉可以改为 update resources
9.运行
点击run,自动访问 http://localhost:port/显示 index.jsp的内容:hello world.
转载于:https://www.cnblogs.com/ICE_Inspire/p/9734435.html