springmvc依賴注入原理 springmvc依賴注入注解



文章插圖
springmvc依賴注入原理 springmvc依賴注入注解

文章插圖
初識SpringMVC
實現步驟:
新建一個web項目導入相關jar包編寫web.xml,注冊DispatcherServlet編寫springmvc配置文件接下來就是去創建對應的控制類 , controller最后完善前端視圖和controller之間的對應測試運行調試使用springMVC必須配置的三大件:
處理器映射器、處理器適配器、視圖解析器
通常,我們只需要手動配置視圖解析器,而處理器映射器和處理器適配器只需要開啟注解驅動即可,而省去了大段的xml配置
注解實現SpringMVC常見注解
@[email protected]@[email protected]控制器
package com.kuang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;[email protected]@[email protected]("/test2")public class [email protected]("/t2")public String index(Model model){//Spring MVC會自動實例化一個Model對象用于向視圖中傳值model.addAttribute("msg", "ControllerTest2");//返回視圖位置return "test";}}@Controller是為了讓Spring IOC容器初始化時自動掃描到;@RequestMapping是為了映射請求路徑,這里因為類與方法上都有映射所以訪問時應該是/test2/t2;標準maven依賴
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.reliable</groupId><artifactId>SpringMVC2</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>springmvc-04-controller</module></modules><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.9.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build></project>一、配置pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>SpringMVC2</artifactId><groupId>com.reliable</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>springmvc-04-controller</artifactId><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build></project>二、配置web.xml
<?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"><!--1.注冊servlet--><servlet><servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--通過初始化參數指定SpringMVC配置文件的位置,進行關聯--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc-servlet.xml</param-value></init-param><!-- 啟動順序,數字越小,啟動越早 --><load-on-startup>1</load-on-startup></servlet><!--所有請求都會被springmvc攔截 --><servlet-mapping><servlet-name>SpringMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>三、配置springmvc-servlet.xml
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 自動掃描包,讓指定包下的注解生效,由IOC容器統一管理 --><context:component-scan base-package="com.kuang.controller"/><!-- 讓Spring MVC不處理靜態資源:html 等--><mvc:default-servlet-handler/><!--支持mvc注解驅動[email protected]?[email protected]必須向上下文中注冊DefaultAnnotationHandlerMapping和一個AnnotationMethodHandlerAdapter實例這兩個實例分別在類級別和方法級別處理 。而annotation-driven配置幫助我們自動完成上述兩個實例的注入 。<mvc:annotation-driven /> 完成了映射和適配(支持用注解完成)--><mvc:annotation-driven /><!-- 視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><!-- 前綴 --><property name="prefix" value="http://www.mnbkw.com/WEB-INF/jsp/" /><!-- 后綴 --><property name="suffix" value="http://www.mnbkw.com/jxjc/167851/.jsp" /></bean></beans>RestFul 風格
概念Restful就是一個資源定位及資源操作的風格 。不是標準也不是協議,只是一種風格 。基于這個風格設計的軟件可以更簡潔,更有層次,更易于實現緩存等機制 。
功能資源:互聯網所有的事物都可以被抽象為資源資源操作:使用POST、DELETE、PUT、GET,使用不同方法對資源進行操作 。分別對應 添加、 刪除、修改、查詢 。
RestFulController(@PathVariable)
package com.kuang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class RestFulController [email protected]("/commit/{p1}/{p2}")public String index(@PathVariable int p1, @PathVariable int p2, Model model){int result = p1+p2;//Spring MVC會自動實例化一個Model對象用于向視圖中傳值model.addAttribute("msg", "結果:"+result);//返回視圖位置return "test";}//映射訪問路徑,[email protected](value = "http://www.mnbkw.com/hello",method = {RequestMethod.GET})public String index2(Model model){model.addAttribute("msg", "hello!");return "test";}}
使用method屬性指定請求類型
用于約束請求的類型,可以收窄請求范圍 。指定請求謂詞的類型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等 。
//映射訪問路徑,[email protected](value = "http://www.mnbkw.com/hello",method = {RequestMethod.GET})public String index2(Model model){model.addAttribute("msg", "hello!");return "test";}除了添加method,還可以使用注解
@[email protected]@[email protected]@PatchMapping
【springmvc依賴注入原理 springmvc依賴注入注解】//映射訪問路徑,[email protected](value = "http://www.mnbkw.com/hello")public String index2(Model model){model.addAttribute("msg", "hello!");return "test";}