SSM框架學習之SpringMVC淺談
什麼是SpringMVC
百度:Spring MVC屬於SpringFrameWork的後續產品,已經融合在Spring Web Flow裡面。Spring 框架提供了構建Web應用程式的全功能 MVC 模組。簡單來說,SpringMVC就是Spring在基於MVC思想的基礎上研發出的框架。
SpringMVC流程
MVC思想:把頁面/瀏覽器發來的請求通過Controller(控制器)呼叫Model(資料模型和業務模型)進行資料封裝和業務處理,然後再把處理後的結果通過View(檢視)展現出來。
SpringMVC的流程:
1. 使用者傳送請求到DispatcherServlet 控制器
2. DispatcherServlet 控制器根據請求路徑到HandlerMapping對映器查詢具體的handler處理器
3. HandlerMapping對映器根據使用者請求查詢與之對應的HandlerExecutionChain執行鏈再回傳給DispatcherServlet控制器
4. DispatcherServlet控制器根據handler具體的實現方式呼叫HandlerAdapter介面卡
5. HandlerAdapter介面卡呼叫具體的handler處理器處理業務並返回ModelAndView到DispatcherServlet控制器
6. DispatcherServlet控制器將ModelAndView專遞到ViewResolver檢視解析器
7. ViewResolver檢視解析器 返回具體的檢視到DispatcherServlet控制器
8. DispatcherServlet控制器渲染檢視後響應給使用者
SpringMVC配置檔案
配置檔案(一般命名:springmvc-servlet.xml)的配置流程就是跟隨著SpringMVC的流程進行配置,DispatcherServlet 是“springframework”下的依賴,不用配置,Model(handler)和View是自己程式碼實現,所以也不用配置,因此只用配置HandlerMapping(對映器)、HandlerAdapter(介面卡)、ViewResolver(檢視解析器)。又因為目前開發一般都會用到“註解”,就需要在配置檔案中添加註解驅動<mvc:annotation-driven />,可以免去HandlerMapping(對映器)、HandlerAdapter(介面卡)。
補充:<mvc:annotation-driven />的作用:https://www.cnblogs.com/afeng2010/p/10133797.html
DispatcherServlet 的父類是FrameworkServlet,點進去看到一段註釋規定了配置檔案的位置和命名規則:預設讀取/WEB-INF/{servlet-name}-servlet.xml這個配置檔案,一般預設為:springmvc-servlet.xml
springmvc-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" xmlns:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 9 <!-- 配置註解驅動,替代推薦使用的對映器以及介面卡,json轉換器 --> 10 <mvc:annotation-driven /> 11 <!-- 開啟註解掃描 --> 12 <context:component-scan base-package="com.demo.controller"></context:component-scan> 13 <!-- 配置檢視解析器 --> 14 <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" --> 15 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 16 <property name="prefix" value="/WEB-INF/views/"></property> 17 <property name="suffix" value=".jsp"></property> 18 </bean> 19 </beans>
專案demo:
專案結構:
pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" 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.demo.springmvc</groupId> 8 <artifactId>springmvc</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>springmvc 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 <dependencies> 23 <dependency> 24 <groupId>org.springframework</groupId> 25 <artifactId>spring-webmvc</artifactId> 26 <version>5.1.7.RELEASE</version> 27 </dependency> 28 <dependency> 29 <groupId>org.slf4j</groupId> 30 <artifactId>slf4j-log4j12</artifactId> 31 <version>1.7.12</version> 32 </dependency> 33 <!-- JSP相關 --> 34 <dependency> 35 <groupId>jstl</groupId> 36 <artifactId>jstl</artifactId> 37 <version>1.2</version> 38 </dependency> 39 <dependency> 40 <groupId>javax.servlet</groupId> 41 <artifactId>servlet-api</artifactId> 42 <version>2.5</version> 43 </dependency> 44 <dependency> 45 <groupId>javax.servlet</groupId> 46 <artifactId>jsp-api</artifactId> 47 <version>RELEASE</version> 48 </dependency> 49 <dependency> 50 <groupId>junit</groupId> 51 <artifactId>junit</artifactId> 52 <version>4.11</version> 53 </dependency> 54 </dependencies> 55 56 <build> 57 <finalName>springmvc</finalName> 58 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 59 <plugins> 60 <plugin> 61 <artifactId>maven-clean-plugin</artifactId> 62 <version>3.1.0</version> 63 </plugin> 64 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 65 <plugin> 66 <artifactId>maven-resources-plugin</artifactId> 67 <version>3.0.2</version> 68 </plugin> 69 <plugin> 70 <artifactId>maven-compiler-plugin</artifactId> 71 <version>3.8.0</version> 72 </plugin> 73 <plugin> 74 <artifactId>maven-surefire-plugin</artifactId> 75 <version>2.22.1</version> 76 </plugin> 77 <plugin> 78 <artifactId>maven-war-plugin</artifactId> 79 <version>3.2.2</version> 80 </plugin> 81 <plugin> 82 <artifactId>maven-install-plugin</artifactId> 83 <version>2.5.2</version> 84 </plugin> 85 <plugin> 86 <artifactId>maven-deploy-plugin</artifactId> 87 <version>2.8.2</version> 88 </plugin> 89 </plugins> 90 </pluginManagement> 91 </build> 92 </project>Pom.xml檔案
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 <servlet> 9 <servlet-name>springmvc</servlet-name> 10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 11 <load-on-startup>1</load-on-startup> 12 </servlet> 13 <servlet-mapping> 14 <servlet-name>springmvc</servlet-name> 15 <!-- 16 /*:攔截所有請求,包括jsp 17 / :攔截所有請求,不包含jsp 18 *.do,*.action 19 --> 20 <url-pattern>*.do</url-pattern> 21 </servlet-mapping> 22 <welcome-file-list> 23 <welcome-file>index.html</welcome-file> 24 <welcome-file>index.htm</welcome-file> 25 <welcome-file>index.jsp</welcome-file> 26 <welcome-file>default.html</welcome-file> 27 <welcome-file>default.htm</welcome-file> 28 <welcome-file>default.jsp</welcome-file> 29 </welcome-file-list> 30 </web-app>Web.xml
index.jsp:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8" %> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 6 </head> 7 <body> 8 <h2>Hello World!</h2> 9 <form action="/hello.do"> 10 <button type="submit" style="height: auto ; width: auto">跳轉</button> 11 </form> 12 </body> 13 </html>index.jsp
hello.jsp:
1 <%@ page language="java" contentType="text/html; charset=utf-8" isELIgnored="false" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- <span style="font-size:30px; color:red;">第一個springmvc程式</span> --> 11 <span style="font-size:30px; color:red;">${msg }</span> 12 </body> 13 </html>Hello.jsp
springmvc-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" xmlns:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 9 <!-- 配置註解驅動,替代推薦使用的對映器以及介面卡,json轉換器 --> 10 <mvc:annotation-driven /> 11 <!-- 開啟註解掃描 --> 12 <context:component-scan base-package="com.demo.controller"></context:component-scan> 13 <!-- 配置檢視解析器 --> 14 <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" --> 15 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 16 <property name="prefix" value="/WEB-INF/views/"></property> 17 <property name="suffix" value=".jsp"></property> 18 </bean> 19 </beans>springmvc-servlet.xml
mycontroller:
1 @Controller 2 public class mycontroller { 3 @RequestMapping("hello") 4 public ModelAndView test() { 5 ModelAndView mv = new ModelAndView(); 6 //設定返回頁面的名稱 7 mv.setViewName("hello"); 8 //設定返回資訊 9 mv.addObject("msg", "HelloWorld!"); 10 return mv; 11 } 12 }MyController
SpringMVC註解
- Controller:表示該類為一個處理器,相當於 mycontroller implements Controller
- RequestMapping(value="hello"),配置方法的對應的url , 預設的url字尾和<url-pattern>*.do</url-pattern> 一致
- GetMapping:相當於RequestMapping(method = RequestMethod.GET)
- PostMapping:相當於RequestMapping(method = RequestMethod.POST)
- PutMapping:相當於RequestMapping(method = RequestMethod.PUT)
- DeleteMapping:相當於RequestMapping(method = RequestMethod.DELETE)