Springmvc

1.解析SpringMVC的請求

  每當用戶在Web瀏覽器中點擊鏈接或者提交表單的時候請求就將開始工作了。


  SpringMVC所有的請求都會通過一個前段控制器Servlet。前段控制器是常用的web應用程序模式,在這裏一個但是實例的servlet將請求委託給應用程序的其他組件來執行實際的處理。在SpringMVC中,DispatcherServlet就是前段控制器。

控制器在完成邏輯處理後,通常會產生一些信息,這些西悉尼需要返回給用戶並在瀏覽器上顯示。這些信息被稱爲模型,

控制器所做的最後一件事就是將模型數據打包,並且表示出用於渲染輸出的視圖名稱。它接下來會將請求連同模型和視圖名稱發送回DispatchServlet

2.springmvc環境搭建

<servlet>
		<servlet-name>user</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:/org/sh/xml/user-servlet.xml</param-value>
		</init-param>
	</servlet>

<servlet-mapping>
		<servlet-name>user</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

通過將DispatcherServlet映射到/聲明瞭他會作爲默認的餓servlet並且會處理所有的請求,包括對靜態資源的請求

Spring的MVC命名空間包含了一個新的<mvc:resource>他會處理靜態資源的請求。只需要在Spring 配置文件中對其進行配置。


2.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc
        	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	<context:component-scan base-package="com.qunar.controller"></context:component-scan>
	<mvc:annotation-driven/>
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>
<mvc:annotation-driven/>
啓動SpringMVC的註解功能

<context:component-scan base-package="com.qunar.controller"></context:component-scan>
自動掃描包

<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

當DispatcherServlet要求InternalResourceViewResolver 解析視圖的時候,他將獲取一個邏輯視圖名稱,添加/WEB-INF/jsp/前綴 和“.jsp”後綴。得到的結果就是渲染輸出的jsp路徑,在內部InternalResourceViewResolver  接下來會將這個路徑傳遞給VIEW對象,VIEW對象將請求傳遞給JSP所以,當HomeController返回home作爲邏輯試圖名稱的時候,他最終將被解析成

/WEB-INF/jsp/home.jsp 路勁

3.Spring 應用上下文

  DispatcherServlet會根據一個XML文件來加載其中Spring應用上下文,而這個文件基於他的<Servlet-name>屬性來確定。

  ContextLoaderListener是一個servlet監聽器,除了DispatcherServlet創建的應用上下文以外,他能夠加載其他的配置文件到一個Spring應用上下文中,爲了是使用它,需要在ContextLoaderListener,需要在web.xml文件中添加如下的<listener>聲明:

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

如果,沒有指定要加載哪些配置文件。上下文加載器就會在/WEB-INFO/applicationContext.xml這個Spring配置文件。但是這個文件本省沒有做到將應用上下文分爲多個片段。所以我們需要重寫默認實現。

爲了給ContextLoaderListener指定一個或多個Spring配置文件,需要在Servlet上下文中配置ContextConfigLocation參數:

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:/org/sh/xml/applicationContext.xml
		</param-value>
	</context-param>


contextConfigLocation參數指定了一個路徑的列表,除非特別聲明,路勁是相對於應用根目錄的,但是我們的Spring配置被分成了多個XML文件,並分散在WEB應用程序的多個JAR文件,所以對其中的一些我們添加了classPath:前綴,使得他們能夠以資源的方式在應用程序中的類路徑中加載,而其他的文件則添加了web應用幾個程序的本地路徑。

 未完待續

4.在Spring中獲取json數據

  1)加入如下依賴

<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-core-asl</artifactId>
		</dependency>

2)編寫Controller

@RequestMapping(value="/json",method=RequestMethod.GET)
	@ResponseBody
	public List<User> getUserJson(){
		List<User> users = null;
		try {
			users = userMapper. selectUsers();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}


3)打開mvc註解功能

<mvc:annotation-driven/>


發佈了93 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章