SpringMVC中@RequestMapping注解用法實例
1 修飾類和方法
package site.exciter.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
/**
* 1. @RequestMapping 除了修飾方法, 還可來修飾類
* 2.
* 1). 類定義處: 提供初步的請求映射信息。相對于 WEB 應用的根目錄
* 2). 方法處: 提供進一步的細分映射信息。 相對于類定義處的 URL。若類定義處未標注 @RequestMapping,則方法處標記的 URL
* 相對于 WEB 應用的根目錄
*/
@RequestMapping("/testRequestMapping")
public String testRequestMapping() {
System.out.println("testRequestMapping");
return "success";
}
}
2 value
@RequestMapping("/springmvc")可以改為@RequestMapping(value = "springmvc")
3 method
post請求
controller
/**
* method比較常用,用來指定請求方式
* @return
*/
@RequestMapping(value = "testMethod", method = RequestMethod.POST)
public String testMethod() {
System.out.println("testMethod");
return "success";
}
jsp
<form action="/springmvc/testMethod" method="post">
<input type="submit" value="submit">
</form>
4 params和headers
/**
* 可以使用 params 和 headers 來更加精確的映射請求. params 和 headers 支持簡單的表達式.
*
* @return
*/
@RequestMapping(value = "testPramsAndHeaders",
params = {"username", "age!=10"}, headers = {})
public String testPramsAndHeaders() {
System.out.println("testPramsAndHeaders");
return "success";
}
<a href="/springmvc/testPramsAndHeaders?username=exciter&age=10" rel="external nofollow" >testPramsAndHeaders</a>
當age=10時,訪問會出錯;當age為其他值時,正常訪問。
http://localhost:8080/springmvc/testPramsAndHeaders?username=exciter&age=10

5 Ant路徑
/**
* 通配符 Ant風格,*可以是任何內(nèi)容
*
* @return
*/
@RequestMapping("/testAntPath/*/exciter")
public String testAndPath() {
System.out.println("testAndPath");
return "success";
}
<a href="/springmvc/testAntPath/ww/exciter" rel="external nofollow" >testAntPath</a>
5 @PathVariable
/**
* @PathVariable 可以來映射 URL 中的占位符到目標方法的參數(shù)中
*/
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id) {
System.out.println("testPathVariable:" + id);
return "success";
}
<a href="/springmvc/testPathVariable/1" rel="external nofollow" >testPathVariable</a>
6 HiddenHttpMethodFilter
HiddenHttpMethodFilter:瀏覽器 form 表單只支持 GET、POST、HEAD 請求,而DELETE、PUT 等 method 并不支 持,Spring3.0 添加了一個過濾器,可以將這些請求轉(zhuǎn)換為標準的 http 方法,使得支持 GET、POST、PUT 與DELETE 請求。
web.xml中配置HiddenHttpMethodFilter
<!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter,可以把 POST 請求轉(zhuǎn)為 DELETE 或 PUT 請求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
public String testRestGet(@PathVariable("id") Integer id) {
System.out.println("testRestGet:" + id);
return "success";
}
@RequestMapping(value = "/testRest", method = RequestMethod.POST)
public String testRestPost() {
System.out.println("testRestPost");
return "success";
}
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
public String testRestPut(@PathVariable Integer id) {
System.out.println("testRestPut:" + id);
return "success";
}
/**
* Rest 風格的 URL. 以 CRUD 為例: 新增: /order POST 修改: /order/1 PUT update?id=1 獲取:
* /order/1 GET get?id=1 刪除: /order/1 DELETE delete?id=1
* <p>
* 如何發(fā)送 PUT 請求和 DELETE 請求呢 ? 1. 需要配置 HiddenHttpMethodFilter 2. 需要發(fā)送 POST 請求
* 3. 需要在發(fā)送 POST 請求時攜帶一個 name="_method" 的隱藏域, 值為 DELETE 或 PUT
* <p>
* 在 SpringMVC 的目標方法中如何得到 id 呢? 使用 @PathVariable 注解
*/
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
public String testRestDelete(@PathVariable Integer id) {
System.out.println("testRestDelete:" + id);
return "success";
}<a href="/springmvc/testRest/1" rel="external nofollow" >
<button>TestRest GET</button>
</a>
<form action="/springmvc/testRest" method="post">
<input type="submit" value="TestRest POST">
</form>
<form action="/springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="TestRest PUT">
</form>
<form action="/springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="TestRest DELETE">
</form>
注意: 在Tomcat8.0以上請求完接口之后返回頁會報錯:
HTTP狀態(tài) 405 - 方法不允許
類型 狀態(tài)報告消息 JSP 只允許 GET、POST 或 HEAD。Jasper 還允許 OPTIONS
描述 請求行中接收的方法由源服務器知道,但目標資源不支持
臨時解決方案:在返回的jsp中添加isErrorPage="true":
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
7 @RequestParam
/**
* @RequestParam 來映射請求參數(shù)
* value 值即請求參數(shù)的參數(shù)名
* required 該參數(shù)是否必須. 默認為 true
* defaultValue 請求參數(shù)的默認值
*/
@RequestMapping("/testRequestParam")
public String testRequestParam(@RequestParam(value = "username") String username,
@RequestParam(value = "age", required = false, defaultValue = "0") int age) {
System.out.println("testRequestParam: username=" + username + " age=" + age);
return "success";
}
<a href="/springmvc/testRequestParam?username=exciter&age=20" rel="external nofollow" rel="external nofollow" >testRequestParam</a>
8 @RequestHeader
/**
* 映射請求頭信息 用法同 @RequestParam
*/
@RequestMapping("/testRequestHeader")
public String testRequestHeader(@RequestHeader(value = "Accept-Language") String al) {
System.out.println("testRequestHeader:" + al);
return "success";
}
<a href="/springmvc/testRequestParam?username=exciter&age=20" rel="external nofollow" rel="external nofollow" >testRequestParam</a>
9 @CookieValue
@RequestMapping("/testCookieValue")
public String testCookieValue(@CookieValue(value = "JSESSIONID") String sessionId) {
System.out.println("testCookieValue:" + sessionId);
return "success";
}總結(jié)
到此這篇關于SpringMVC中@RequestMapping注解用法的文章就介紹到這了,更多相關SpringMVC @RequestMapping用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java算法入門之有效的括號刪除有序數(shù)組中的重復項實現(xiàn)strStr
大家好,我是哪吒,一個熱愛編碼的Java工程師,本著"欲速則不達,欲達則欲速"的學習態(tài)度,在程序猿這條不歸路上不斷成長,所謂成長,不過是用時間慢慢擦亮你的眼睛,少時看重的,年長后卻視若鴻毛,少時看輕的,年長后卻視若泰山,成長之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程2021-08-08
java int類型二維數(shù)組實現(xiàn)“楊輝三角”的完整實例
這篇文章主要給大家介紹了關于java int類型二維數(shù)組實現(xiàn)“楊輝三角”的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12

