1.概述 1.1 SpringMVC使用@RequestMapping註解為控制器指定可以處理哪些URL請求; 1.2 在控制器的類定義及方法定義處都可以標註@RequestMapping; 1.2.1 類定義處標註:提供初步的請求映射信息。相對於WEB應用的根目錄; 1.2.2 方法處標註: 提供 ...
1.概述
1.1 SpringMVC使用@RequestMapping註解為控制器指定可以處理哪些URL請求;
1.2 在控制器的類定義及方法定義處都可以標註@RequestMapping;
1.2.1 類定義處標註:提供初步的請求映射信息。相對於WEB應用的根目錄;
1.2.2 方法處標註: 提供進一步的細分映射信息。相對於類定義處的URL;
若類定義處未標註,則方法處標記的URL相對於WEB應用的根目錄;
1.3 DispatcherServlet截取請求後,就通過控制器上@RequestMapping提供的映射信息確定請求所對應的處理方法;
2.代碼驗證
Test類: package com.yk.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/springmvc") @Controller public class SpringMVCTest { private static final String SUCCESS = "success"; @RequestMapping("/testRequestMapping") public String testRequestMapping(){ System.out.println("SpringMVCTest.testRequestMapping()"); // return "success"; 下麵會有很多,所以定義一個常量 return SUCCESS; } } index.jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <a href="springmvc/testRequestMapping">Test--RequestMapping</a> <br /><br /> <a href="helloworld">Hello World</a> </body> </html>
3.瀏覽器請求地址
1. http://localhost:8080/SPRING-MVC-01/ 2. http://localhost:8080/SPRING-MVC-01/springmvc/testRequestMapping