SpringMVC——常用註解說明

RequestParam

  • 作用:把請求中指定名稱的參數賦值給控制器中的形參。
  • 常用屬性:
屬性 功能
value 請求參數中的名稱賦值給方法形參 與name屬性互爲別名
name 請求參數中的名稱賦值給方法形參 與value屬性互爲別名
required 請求參數中是否必須傳入此參數。默認值:true,表示必須提供,如果不提供將報錯

示例代碼

編寫jsp代碼

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--requestParams註解的使用-->
        <a href="springmvc/useRequestParam?name=test">測試 requestParam註解</a>
    </body>
</html>

編寫Controller代碼

package com.liang.controller;

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

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/useRequestParam")
    public String useRequestParam(@RequestParam(name = "name") String username, @RequestParam(value = "age", required = false) Integer age){
        System.out.println("執行了useRequestParam方法..."+username+" : "+age);
        return "success";
    }
}

RequestBody

  • 作用:用於獲取請求體內容,直接使用得到key=value&key=value……結構的數據。get請求方式不適用。
  • 常用屬性:
屬性 功能
required 是否必須有請求體,默認值:true,此時get請求方式會報錯,若值爲false時,get請求爲null

示例代碼

編寫jsp代碼

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--RequestBody註解的使用-->
        <!--post請求方式-->
        <form action="springmvc/useRequestBody" method="post">
            用戶名稱:<input type="text" name="username"/><br/>
            用戶密碼:<input type="password" name="password"/><br/>
            用戶年齡:<input type="text" name="age"/><br/>
            <input type="submit" value="保存"/>
        </form>
        <!--get請求方式-->
        <a href="springmvc/useRequestBody?body=test">測試 RequestBody 註解 get請求</a>
    </body>
</html>

編寫Controller代碼

package com.liang.controller;

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

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/useRequestBody")
    public String useRequestBody(@RequestBody(required = false) String body){
        System.out.println("執行了useRequestBody方法..."+body);
        return "success";
    }
}

PathVaribale

  • 作用:用於綁定URL中的佔位符,Spring3.0後加入了url支持佔位符
  • 常用屬性:
屬性 功能
value 用於指定url中佔位符名稱
required 是否必須提供佔位符

示例代碼

編寫jsp代碼

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--PathVariable註解-->
        <a href="springmvc/usePathVariable/100/200">PathVariable註解</a>
    </body>
</html>

編寫Controller代碼

package com.liang.controller;

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

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/usePathVariable/{id}/{i}")
    public String usePathVariable(@PathVariable("id") Integer id , @PathVariable("i") Integer i){
        System.out.println("執行了usePathVariable方法...  id=" + id + " , i="+i);
        return "success";
    }
}

RequestHeader

  • 作用:用於獲取請求消息頭
  • 常用屬性:
屬性 功能
value/name 需要獲取到的消息頭名稱
required 是否必須有此消息頭

示例代碼

編寫jsp代碼

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--RequestHeader註解-->
        <a href="springmvc/useRequestHeader">獲取請求消息頭</a>
    </body>
</html>

編寫Controller代碼

package com.liang.controller;

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

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/useRequestHeader")
    public String useRequestHeader(@RequestHeader(name = "Accept-Language", required = false) String requestHeader){
        System.out.println("執行了useRequestHeader方法...  "+requestHeader );
        return "success";
    }
}

CookieValue

  • 作用:用於把指定cookie名稱的值傳入控制器方法參數中。
  • 常用屬性
屬性 功能
value 指定cookie的名稱
required 是否必須有此cookie

示例代碼

編寫jsp代碼

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--CookieValue註解-->
        <a href="springmvc/useCookieValue">獲取cookie的值</a>
    </body>
</html>

編寫Controller代碼

package com.liang.controller;

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

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/useCookieValue")
    public String useCookieValue(@CookieValue(value = "JSESSIONID", required = false) String cookieValue){
        System.out.println("執行了useCookieValue方法...  " + cookieValue );
        return "success";
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章