【Spring】SpringMVC框架理解

一、配置步驟

  1. 配置web.xml
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:spring-*.xml</param-value>
</context-param>

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
	<servlet-name>dispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-*.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>dispatcherServlet</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>
  1. 創建spring-mvc配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="com.lanou">
		<!-- 設置配置包掃描時所需要排除或所要掃描的內容 -->
		<!-- 	
		<context:include-filter type="annotation" expression=""/>
		<context:exclude-filter type="annotation" expression=""/>
		 -->
	</context:component-scan>

	<!-- 開啓SpringMVC註解 -->
	<mvc:annotation-driven></mvc:annotation-driven>

 	
 	<!-- JSP試圖解析器配置  -->
 	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 		<property name="prefix" value="/jsp/"></property>
 		<property name="suffix" value=".jsp"></property>
 	</bean>

</beans>

二、原理

SpringMVC原理圖

SpringMVC執行流程一般有以下幾個步驟:

  1. 用戶發起請求,網絡容器查找web.xml,最終將請求轉給DispatcherServlet
  2. DispatcherServlet將請求交給HandlerMapping,HandlerMapping解析映射路徑以及與之對應的類和方法,並將解析出來的信息返回給DispatcherServlet
  3. DispatcherServlet將映射關係交給HandlerAdapter,調用Handler執行對象相應的方法,將執行數據封裝成ModelAndView返回給DispatcherServlet
  4. DispatcherServlet將ModelAndView交給ViewResolver解析視圖併爲視圖填充相應的數據,最後對視圖進行返回給DispatcherServlet
  5. 最終將視圖返回給用戶

三、註解

註解 常用屬性 作用 註解級別
@Controller 一般不使用參數 負責處理由DispatcherServlet分發的請求 類級
@Service 一般不使用參數 對應業務層 類級
@Repository 一般不使用參數 對應數據訪問層 類級
@Component 一般不使用參數 通用註解,不明確哪一層時使用 類級
@RequestMapping value[default],method 用以處理請求地址映射的分解 類級、方法級
@ResponseBody 一般不使用參數 設置方法或類返回的數據格式爲JSON 類級、方法級
@SessionAttributes value、types 綁定HttpSession中的attribute對象的值 方法級
@ModelAttribute 一般不使用參數 用於制定model對象 方法級、參數級
@PathVariable value[default] 用於取出uri模板中的變量作爲參數 參數級
@RequestParam value[default] 用於獲取接口調用時所需參數值 參數級

四、自動裝配註解

註解 裝配方式 所屬體系 標準 類全路徑
@Autowired 按類型注入 SPRING SPRING2.5 org.springframework.beans.factory.annotation.Autowired
@Inject 按類型注入 JAVA JSR330 javax.inject
@Qualifier 按名稱注入 SPRING SPRING2.5 org.springframework.beans.factory.annotation.Autowired
@Resource 按名稱注入 JAVA JSR250 javax.annotation.Resource

五、數據對象

  1. DTO(數據傳輸對象 Data Transfer Object):用於數據傳輸的對象
  2. DAO(數據訪問對象 Data Access Object):用於定義數據庫訪問
  3. PO(持久化對象 Presistence Object):和數據庫中數據表字段一一對應
  4. POJO(簡單普通Java對象 Plain Ordinary Java Object):一般Java類
  5. VO(試圖對象 View Object):用於在視圖上進行渲染
發佈了73 篇原創文章 · 獲贊 282 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章