springmvc學習(二)——使用RequestMapper請求映射

來源:互聯網
上載者:User

標籤:

本次內容是@RequestMapping,後面會有執行個體代碼

Spring MVC 使用 @RequestMapping 註解為控制器指定可以處理哪些 URL 請求在控制器的類定義及方法定義處都可標註@RequestMapping

@RequestMapping不僅可以修飾類,還可以修飾方法。
類定義處:提供初步的請求映射資訊。相對於 WEB 應用的根目錄
方法處:提供進一步的細分映射資訊。相對於類定義處的 URL。若類定義處未標註 @RequestMapping,則方法處標記的 URL 相對於WEB 應用的根目錄
DispatcherServlet 截獲請求後,就通過控制器上 @RequestMapping 提供的映射資訊確定請求所對應的處理方法。

映射請求參數、要求方法或要求標頭

@RequestMapping 除了可以使用請求 URL 對應請求外, 還可以使用要求方法、請求參數及要求標頭映射請求
@RequestMapping 的 value、method、params 及 headers 分別表示請求 URL、要求方法、請求參數及要求標頭的映射條件,他們之間是與的關係,聯合使用多個條件可讓請求映射更加精確化。
params 和 headers支援簡單的運算式:
param1: 表示請求必須包含名為 param1 的請求參數
!param1: 表示請求不能包含名為 param1 的請求參數
param1 != value1: 表示請求包含名為 param1 的請求參數,但其 值不能為 value1
{“param1=value1”, “param2”}: 請求必須包含名為 param1 和param2 的兩個請求參數,且 param1 參數的值必須為 value1

執行個體代碼

@RequestMapping("/springmvc")@Controllerpublic class requestmapper {private static final  String SUCCESS="success";@RequestMapping(value="/testparamheader" ,params={"username","age!=10"},headers="Accept-Language=zh-CN,zh;q=0.8")public String testParam(){System.out.println("testParam");return SUCCESS;}@RequestMapping(value="/testMethod" ,method=RequestMethod.POST)public String testMethod(){System.out.println("testMethod");return SUCCESS;}@RequestMapping("/requestmap")public String requestmap(){System.out.println("RequestMapper");return SUCCESS;}}

 index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    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=ISO-8859-1"><title>Insert title here</title></head><body><a href="springmvc/testparamheader?username&age=11">testparamheader</a><form action="springmvc/testMethod" method="post"><input type="submit" value="submit"></form><a href="helloworld">hello world</a><a href="springmvc/requestmap">requestmap</a></body></html>

 結果還是全部映射到success.jsp的物理視圖

 

springmvc學習(二)——使用RequestMapper請求映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.