SpringMVC是Spring的一個組件,所以我們在使用SpringMVC的時候也會使用到Spring
使用環境
- JDK:1.8
- Tomcat:9.0.3
- spring:5.2.8
- 編譯器:IDEA2019
1、導包
需要引入Spring-web和Spring-webmvc兩個包,可以到maven倉庫里面去下載或者使用maven依賴
2、ApplicationContext.xml配置(Spring的核心配置文件)
- ApplicationContext.xml文件需要放在WEB-INF下,并且需要把名字改為攔截的serlvet-name+ -Servlet,比如我這邊的攔截名字為mvc,所以我需要把配置文件名改為mvc-Servlet.xml
- 如果不放在WEB-INF下,需要在web.xml文件中進行路徑配置(如下web.xml文件中的init-param標簽配置)
- 注意命名空間的問題
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xmlns:mvc = "http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 開啟spring注解驅動--> < context:component-scan base-package = "com.cjh" /> <!-- 開啟mvc注解驅動--> < mvc:annotation-driven ></ mvc:annotation-driven > </ beans > |
3、web.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version = "4.0" > < servlet > < servlet-name >mvc</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > <!-- 說明Spring核心配置文件的位置--> < param-value >classpath:ApplicationContext.xml</ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name >mvc</ servlet-name > < url-pattern >*.do</ url-pattern > </ servlet-mapping > </ web-app > |
4、java的實現
Controller類
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Controller @RequestMapping ( "userController.do" ) public class UserController { public UserController(){ System.out.println( "controller創建了" ); } @RequestMapping public void test(){ System.out.println( "controller:test方法執行了" ); } } |
index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page contentType="text/html; charset=UTF-8" language="java" %> < html > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >cai jin hong</ title > < style > </ style ></ head > < body > < a href = "userController.do" rel = "external nofollow" >測試</ a > </ body > </ html > |
請求和響應流程:
- 當點擊測試超鏈接時,瀏覽器向服務器發送userController.do的資源請求
- 服務器接收到之后,找到類上面帶有@RequestMapping("userController.do")注解的對象
- 找到了之后,查找方法上面帶有@RequestMapping("xxx")注解的方法
如果只有一個方法,可以不用寫名字,直接寫RequestMapping
如果有多個方法,需要注明方法名
- 找到了之后,執行方法,并將處理信息響應回給瀏覽器(該代碼中沒有返回值)
本篇文章只講了一下最基本的時候,下一篇文章會詳細的說的SpringMVC請求和響應的處理!!!
以上就是SpringMVC---配置與使用的示例的詳細內容,更多關于SpringMVC---配置與使用的資料請關注服務器之家其它相關文章!
原文鏈接:https://segmentfault.com/a/1190000025187675?utm_source=tuicool&utm_medium=referral