欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring入门(三) 使用spring mvc

程序员文章站 2022-07-10 13:43:21
1.建立project / module 新建空的project:springMvcStudy 新建module:type maven-webapp,名字mvcStudy 2.为module设置Sources和Resources 在mvcStudy/src/main下新建2个文件夹:java,res ......

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,增加依赖:

spring入门(三) 使用spring mvc
 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.