1. 程式人生 > >第七章 SpringMVC構建WEB應用

第七章 SpringMVC構建WEB應用

 Spring MVC的請求流程:

        第一步:發起請求到前端控制器(DispatcherServlet)

        第二步:前端控制器請求HandlerMapping查詢Handler可以根據xml配置、註解進行查詢

        第三步:處理器對映器HandlerMapping向前端控制器返回Handler

        第四步:前端控制器呼叫處理器介面卡去執行Handler

        第五步:處理器介面卡去執行Handler

        第六步:Handler執行完成給介面卡返回ModelAndView

        第七步:處理器介面卡向前端控制器返回ModelAndView

ModelAndViewspringmvc框架的一個底層物件,包括 Modelview

        第八步:前端控制器請求檢視解析器去進行檢視解析,根據邏輯檢視名解析成真正的檢視(jsp)

        第九步:檢視解析器向前端控制器返回View

        第十步:前端控制器進行檢視渲染。檢視渲染將模型資料(ModelAndView物件中)填充到request

        第十一步:前端控制器向用戶響應結果

專案例項

        1. 建立專案,引入SpringMVCjar

        2. 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 載入前端控制器 -->
  <servlet>
  	<!-- 指定路徑下的請求由DispatcherServlet統一管理分發 -->
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 設定初始化引數:載入配置檔案 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:SpringMVC.xml</param-value>
  	</init-param>
  </servlet>
  
  <!-- 指定訪問路徑及對映關係 -->
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<!-- 訪問路徑字尾為.do時,由分發器類統一分發請求 -->
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
</web-app>

        3. SpringMVC.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:p="http://www.springframework.org/schema/p"
	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/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd"	
	>
	<!-- ↑ 新增context約束 -->

	<!-- 指定分發器在該包下檢索控制類 -->
	<context:component-scan base-package="com.controller"></context:component-scan>

</beans>

        4. 宣告控制類

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

// @Controller宣告類為控制類
@Controller
// @RequestMapping(訪問路徑):指定該類處理
@RequestMapping("/hello")
public class HelloWorld {
	
	// 指定訪問該路徑時,由該方法響應操作
	@RequestMapping("/now.do")
	public String now(){
		System.out.println("墨漸生微:SpringMVC載入成功");
		return null;
	}
}

SpringMVC頁面跳轉

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/*
 * 	請求轉發和重定向:通過方法返回值跳轉頁面
 */
@Controller
@RequestMapping("/hello")
public class HelloWorld {
	
	// 指定訪問該路徑時,由該方法響應操作
	@RequestMapping("/now.do")
	public String now(){
		System.out.println("預設請求轉發");
		// 預設:請求轉發
		return "/index.jsp";
	}
	
	@RequestMapping("/future.do")
	public String future(){
		System.out.println("請求轉發");
		// 請求轉發
		return "forward:/index.jsp";
	}
	
	@RequestMapping("/past.do")
	public String past(){
		System.out.println("重定向");
		// 重定向
		return "redirect:/index.jsp";
	}
}

Servlet頁面跳轉

package com.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/*
 * 	請求轉發和重定向:通過Servelt頁面跳轉
 */
@Controller
@RequestMapping("/hello")
public class HelloWorld {
	
	@RequestMapping("/future.do")
	public void future(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		System.out.println("請求轉發");
		// 請求轉發
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
	
	@RequestMapping("/past.do")
	public void past(HttpServletRequest request,HttpServletResponse response) throws IOException{
		System.out.println("重定向");
		// 重定向
		response.sendRedirect(request.getContextPath()+"/index.jsp");
	}
}