1. 程式人生 > >SpringMVC筆記系列(2)——@RequestMapping請求對映物理檢視解析

SpringMVC筆記系列(2)——@RequestMapping請求對映物理檢視解析

上一篇的例子介紹瞭如何構建一個springMVC的請求對映的方法。但是@RequestMapping不僅可以修飾控制器類的方法,還可以可以修飾控制器類本身,它對請求的影響是:請求url的分段。

還是看一個例子吧。

假如現在有一個請求頁面index2.jsp。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>RequestMapping Test</title> </head> <body> <a href="c1/m1">c1/m1</a><br/> <a href="c1/m2">c1/m2</a><br/> <a href="c1/m3">c1/m3</a>
<br/> </body> </html>

另有3個響應頁面robot_baymax1.jsp、robot_baymax2.jsp、robot_baymax3.jsp在webapp的views資料夾下。

這裡寫圖片描述

現在我們要做的就是用springMVC的DispatcherServlet來處理 請求頁面的請求 到 響應頁面的對映。

關於對映的詳細過程請看上一篇。

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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 配置自動掃描的包 --> <context:component-scan base-package="com.happyBKs.springmvc.handlers"></context:component-scan> <!-- 配置檢視解析器:如何把handler方法 的返回值 解析為 實際的物理檢視--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>

重點來了,我重新構造一個控制器類,來處理這種多段的請求。

定義控制器類RobotHandler。

@RequestMapping除了修飾方法,還可以來修飾類

類定義處:提供初步的請求對映資訊。相對於web應用的根目錄

方法定義處:進一步提供細分的對映資訊。相對於類定義處的URL。若類定義處沒有標註@RequestMapping,則方法處的@RequestMapping直接對應於web應用的根目錄。

這裡做個羅列,可能看得更清楚:

請求:(網站域名+web應用名)web應用根目錄+類定義處@RequestMapping+方法定義處@RequestMapping

對映到
物理檢視:webapp根目錄+springmvc.xml配置的prefix+ 控制器方法的返回值 +springmvc.xml配置的sufix

這裡的話是
請求:http://localhost:8080/mymvc/c1/m1
對映到
物理檢視webapp/views/robot_baymax1.jsp就通過控制器上的

對映過程是:DispatchServlet在截獲請求後,@RequestMapping提供額對映資訊確認請求所對應對的處理方法。

package com.happyBKs.springmvc.handlers;

import java.lang.annotation.Retention;

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

/*
1.@RequestMapping除了修飾方法,還可以來修飾類
類定義處:提供初步的請求對映資訊。相對於web應用的根目錄
方法定義處:進一步提供細分的對映資訊。相對於類定義處的URL。若類定義處沒有標註@RequestMapping,則方法處的@RequestMapping直接對應於web應用的根目錄

請求:(網站域名+web應用名)web應用根目錄+類定義處@RequestMapping+方法定義處@RequestMapping
對映到
物理檢視:webapp根目錄+springmvc.xml配置的prefix+ 控制器方法的返回值 +springmvc.xml配置的sufix


這裡的話是
請求:http://localhost:8080/mymvc/c1/m1
對映到
物理檢視webapp/views/robot_baymax1.jsp就通過控制器上的


對映過程是:DispatchServlet在截獲請求後,@RequestMapping提供額對映資訊確認請求所對應對的處理方法。
*/
@RequestMapping("/c1")
@Controller
public class RobotHandler {

    @RequestMapping("/m1")
    String handle1()
    {
        return "robot_baymax1";
    }

    @RequestMapping("/m2")
    String handle2()
    {
        return "robot_baymax2";
    }


    @RequestMapping("/m3")
    String handle3()
    {
        return "robot_baymax3";
    }
}