欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC中@RequestMapping注解用法實例

 更新時間:2022年06月23日 10:23:46   作者:木水Code  
通過@RequestMapping注解可以定義不同的處理器映射規(guī)則,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

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 應(yīng)用的根目錄
     * 2). 方法處: 提供進一步的細分映射信息。 相對于類定義處的 URL。若類定義處未標(biāo)注 @RequestMapping,則方法處標(biāo)記的 URL
     * 相對于 WEB 應(yīng)用的根目錄
     */
    @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>

當(dāng)age=10時,訪問會出錯;當(dāng)age為其他值時,正常訪問。

http://localhost:8080/springmvc/testPramsAndHeaders?username=exciter&age=10

5 Ant路徑

 /**
     * 通配符 Ant風(fēng)格,*可以是任何內(nèi)容
     *
     * @return
     */
    @RequestMapping("/testAntPath/*/exciter")
    public String testAndPath() {
        System.out.println("testAndPath");
        return "success";
    }
&lt;a href="/springmvc/testAntPath/ww/exciter" rel="external nofollow" &gt;testAntPath&lt;/a&gt;

5 @PathVariable

    /**
     * @PathVariable 可以來映射 URL 中的占位符到目標(biāo)方法的參數(shù)中
     */
    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable("id") Integer id) {
        System.out.println("testPathVariable:" + id);
        return "success";
    }
&lt;a href="/springmvc/testPathVariable/1" rel="external nofollow" &gt;testPathVariable&lt;/a&gt;

6 HiddenHttpMethodFilter

HiddenHttpMethodFilter:瀏覽器 form 表單只支持 GET、POST、HEAD 請求,而DELETE、PUT 等 method 并不支 持,Spring3.0 添加了一個過濾器,可以將這些請求轉(zhuǎn)換為標(biāo)準(zhǔ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 風(fēng)格的 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 的目標(biāo)方法中如何得到 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

描述 請求行中接收的方法由源服務(wù)器知道,但目標(biāo)資源不支持

臨時解決方案:在返回的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é)

到此這篇關(guān)于SpringMVC中@RequestMapping注解用法的文章就介紹到這了,更多相關(guān)SpringMVC @RequestMapping用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java源碼解析HashMap簡介

    Java源碼解析HashMap簡介

    今天小編就為大家分享一篇關(guān)于Java源碼解析HashMap簡介,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • java生成圖片驗證碼功能

    java生成圖片驗證碼功能

    最近在用ssm框架做一個管理系統(tǒng),做到登錄驗證時,需要實現(xiàn)圖片驗證碼功能,今天小編給大家分享基于java制作的圖片驗證碼功能,感興趣的朋友一起看看吧
    2019-12-12
  • java微信開發(fā)中的地圖定位功能

    java微信開發(fā)中的地圖定位功能

    本文通過實例代碼給大家介紹了java微信開發(fā)中的地圖定位功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • java算法入門之有效的括號刪除有序數(shù)組中的重復(fù)項實現(xiàn)strStr

    java算法入門之有效的括號刪除有序數(shù)組中的重復(fù)項實現(xiàn)strStr

    大家好,我是哪吒,一個熱愛編碼的Java工程師,本著"欲速則不達,欲達則欲速"的學(xué)習(xí)態(tài)度,在程序猿這條不歸路上不斷成長,所謂成長,不過是用時間慢慢擦亮你的眼睛,少時看重的,年長后卻視若鴻毛,少時看輕的,年長后卻視若泰山,成長之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程
    2021-08-08
  • 詳解Spring中的@PropertySource注解使用

    詳解Spring中的@PropertySource注解使用

    這篇文章主要介紹了Spring的@PropertySource注解使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • java int類型二維數(shù)組實現(xiàn)“楊輝三角”的完整實例

    java int類型二維數(shù)組實現(xiàn)“楊輝三角”的完整實例

    這篇文章主要給大家介紹了關(guān)于java int類型二維數(shù)組實現(xiàn)“楊輝三角”的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Spring中的AOP原理與使用詳解

    Spring中的AOP原理與使用詳解

    這篇文章主要介紹了Spring中的AOP原理與使用詳解,AOP意為面向切面編程,可以通過預(yù)編譯方式或運行期動態(tài)代理實現(xiàn)在不修改源代碼的情況下給程序動態(tài)統(tǒng)一添加功能的一種技術(shù),需要的朋友可以參考下
    2023-12-12
  • 深入理解Java設(shè)計模式之簡單工廠模式

    深入理解Java設(shè)計模式之簡單工廠模式

    這篇文章主要介紹了JAVA設(shè)計模式之簡單工廠模式的的相關(guān)資料,文中示例代碼非常詳細,供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2021-11-11
  • JVM內(nèi)存結(jié)構(gòu)相關(guān)知識解析

    JVM內(nèi)存結(jié)構(gòu)相關(guān)知識解析

    這篇文章主要介紹了JVM內(nèi)存結(jié)構(gòu)相關(guān)知識解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • java使用ffmpeg處理視頻的方法

    java使用ffmpeg處理視頻的方法

    這篇文章主要介紹了java使用ffmpeg處理視頻的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03

最新評論