spring入門(三) 使用spring mvc

1.創建project / module

新建空的project:springMvcStudy
新建module:type maven-webapp,名字mvcStudyjava

2.爲module設置Sources和Resources

在mvcStudy/src/main下新建2個文件夾:java,resources.
打開File/Project Structure/Project Settings/Modules 選擇mvcStudy,點擊Sources選項卡
設置java文件夾爲Sources,設置resources爲Resources.web

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; 測試類以下:spring

 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 resourcesexpress

9.運行

點擊run,自動訪問 http://localhost:port/
顯示 index.jsp的內容:hello world.spring-mvc

相關文章
相關標籤/搜索