日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

SpringMVC常用注解詳解

 印度阿三17 2021-01-25

常用注解

RequestParam

作用:把請求中指定名稱的參數(shù)給控制器中的形參賦值。

屬性:

  1. value:請求參數(shù)的名稱
  2. required:請求參數(shù)中是否必須提供此參數(shù),默認(rèn)值為true表示必須提供。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>常用注解的使用</title>
</head>
<body>
    <a href="/anno/testRequestParam?name=hehe">RequestParam</a>
</body>
</html>
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/anno")
public class AnnoController {
    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam("name") String username){
        System.out.println(username);
        return "success";
    }
}

RequestBody

作用:用于獲取請求體內(nèi)容,直接使用得到的是key=value&key=value...結(jié)構(gòu)的數(shù)據(jù)。get請求方式不適用。

屬性:required,是否必須有請求體,默認(rèn)是true,當(dāng)取值為true時(shí),get方式會(huì)報(bào)錯(cuò),如果為false,get請求得到的是null。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>常用注解的使用</title>
</head>
<body>
<form action="/anno/testRequestBody" method="post">
    用戶姓名:<input type="text" name="username"><br>
    用戶年齡:<input type="text" name="password"><br>
    <input type="submit" value="提交">
</form>
</body>
</html
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/anno")
public class AnnoController {
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String body){
        System.out.println(body);
        return "success";
    }
}

PathVariable

作用:用于綁定url中的占位符,例如:請求url中/delete/{id},這個(gè){id}就是url的一個(gè)占位符。

屬性:

  1. value:用于指定url中占位符的名稱。
  2. required:是否必須提供占位符。

restful編程風(fēng)格:描述了一個(gè)架構(gòu)樣式的網(wǎng)絡(luò)系統(tǒng),比如web應(yīng)用程序。HTTP協(xié)議是一種無狀態(tài)的協(xié)議,即所有的狀態(tài)都保存在服務(wù)器端,因此,如果客戶端想要操作這樣的服務(wù)器,必須通過某種手段,讓服務(wù)器發(fā)生狀態(tài)轉(zhuǎn)換,而這種轉(zhuǎn)換是建立在表現(xiàn)層(把資源具體呈現(xiàn)出來的形式)之上的,所以就是表現(xiàn)層狀態(tài)轉(zhuǎn)換。具體說,就是HTTP協(xié)議中,四個(gè)表示操作的動(dòng)詞:GET,POST,PUT,DELETE。它們分別對應(yīng)四個(gè)基本操作:GET用來獲取資源,POST用來新建資源,PUT用來更新資源,DELETE用來刪除資源。

restful的示例:

  1. /account/1,HTTP GET:獲取id=1的account
  2. /account/1,HTTP DELETE:刪除id=1的account
  3. /account/1,HTTP PUT:更新id=1的account
  4. /account,HTTP POST:新增account
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>常用注解的使用</title>
</head>
<body>
<a href="/anno/testPathVariable/10">testPathVariable</a>
</body>
</html>
package com.example.controller;

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

@Controller
@RequestMapping("/anno")
public class AnnoController {
    @RequestMapping("/testPathVariable/{sid}") //sid和下面PathVariable的參數(shù)必須一致
    public String testPathVariable(@PathVariable("sid") String id){
        System.out.println(id);
        return "success";
    }
}

RequestHeader

作用:用于獲取請求頭

屬性:

  1. value:提供消息頭名稱。例如:@RequestHeader(value="Accept")
  2. required:是否必須有請求頭。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>常用注解的使用</title>
</head>
<body>
<a href="/anno/testRequestHeader">testRequestHeader</a>
</body>
</html>
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/anno")
public class AnnoController {
    @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "Accept") String header){
        System.out.println(header);
        return "success";
    }
}

CookieValue

作用:用于把指定cookie名稱的值傳入控制器方法參數(shù)。

屬性:

  1. value:指定cookie的名稱。
  2. required:是否必須有此cookie。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>常用注解的使用</title>
</head>
<body>
<a href="/anno/testCookieValue">testCookieValue</a>
</body>
</html>
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/anno")
public class AnnoController {
    @RequestMapping("testCookieValue")
    public String testCookieValue(@CookieValue("JSESSIONID") String cookie){
        System.out.println(cookie);
        return "success";
    }
}

ModelAttribute

作用:出現(xiàn)在方法上,表示當(dāng)前方法會(huì)在控制器的方法執(zhí)行之前先運(yùn)行,它可以修飾沒有返回值的方法,也可以修飾有返回值的方法。出現(xiàn)在參數(shù)上,獲取指定的數(shù)據(jù)給當(dāng)前參數(shù)賦值。

屬性:value用于獲取數(shù)據(jù)的key,key可以是POJO的屬性名稱,也可以是map結(jié)構(gòu)的key。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>常用注解的使用</title>
</head>
<body>
<form action="/anno/testModelAttribute" method="post">
    用戶姓名:<input type="text" name="uname"><br>
    用戶年齡:<input type="text" name="age"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>
//有返回值的方式
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/anno")
public class AnnoController {
    @RequestMapping("testModelAttribute")
    public String testModelAttribute(User user){
        System.out.println(user);
        System.out.println("testModelAttribute執(zhí)行了");
        return "success";
    }

    @ModelAttribute
    public User showUser(String uname){//提交表單不完整時(shí)使用
        User user =new User();
        user.setUname(uname);
        user.setAge(20);
        user.setDate(new Date());
        System.out.println("showUser執(zhí)行了");
        return user;
    }
}
//無返回值的方式
package com.example.controller;
import com.example.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;
@Controller
@RequestMapping("/anno")
public class AnnoController {
    @RequestMapping("testModelAttribute")
    public String testModelAttribute(@ModelAttribute("abc") User user){
        System.out.println(user);
        System.out.println("testModelAttribute執(zhí)行了");
        return "success";
    }

    @ModelAttribute //注意:獲取前端的參數(shù),必須和標(biāo)簽的name屬性一致
    public void showUser(String uname, Map<String,User> map) {//提交表單不完整時(shí)使用
        User user = new User();
        System.out.println(uname);
        user.setUname(uname);
        user.setAge(20);
        user.setDate(new Date());
        map.put("abc",user);
    }
}

SessionAttributes

作用:用于多次執(zhí)行控制器方法間的參數(shù)共享,該注解只能作用在類上。

屬性:

  1. value:用于指定存入的屬性名稱。
  2. type:用于指定存入數(shù)據(jù)的類型。
package com.example.controller;
import com.example.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/anno")
@SessionAttributes(value = {"msg"})
public class AnnoController {
    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Model model){ //Model中存儲(chǔ)數(shù)據(jù)實(shí)際上放入request域中
        model.addAttribute("msg","消息123");
        return "success";
    }
    @RequestMapping("/getSessionAttributes")
public String getSessionAttributes(ModelMap model){ //Model中存儲(chǔ)數(shù)據(jù)實(shí)際上放入request域中
    String msg = (String) model.get("msg");
    System.out.println(msg);
    return "success";
}
@RequestMapping("/delSessionAttributes")
public String delSessionAttributes(SessionStatus status){ //刪除session的內(nèi)容
    status.setComplete();
    return "success";
}
}
<!--發(fā)送請求-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>常用注解的使用</title>
</head>
<body>
<a href="/anno/testSessionAttributes">testSessionAttributes</a>
<a href="/anno/getSessionAttributes">getSessionAttributes</a>
<a href="/anno/delSessionAttributes">delSessionAttributes</a>
</body>
</html>
<!--獲取session中的值-->
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>成功</h3>
${msg}
${sessionScope}
</body>
</html>
來源:https://www./content-4-833751.html

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多