最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC請求參數(shù)的使用總結(jié)

 更新時(shí)間:2021年06月01日 09:48:03   作者:寒詠哥  
在日常使用SpringMVC進(jìn)行開發(fā)的時(shí)候,有可能遇到前端各種類型的請求參數(shù),本文主要接介紹了SpringMVC請求參數(shù)的使用總結(jié),感興趣的可以了解一下

本次數(shù)據(jù)請求使用postman, postman下載地址:https://www.getpostman.com/

一、頁面跳轉(zhuǎn)

1. 頁面跳轉(zhuǎn)

@Controller
public class IndexController {
 
    /**
     * 進(jìn)入首頁
     *
     * @return 首頁頁面
     */
    @RequestMapping("/")
    public String index(){
        return "/index";
    }
}

2. 請求轉(zhuǎn)發(fā)

@Controller
public class IndexController {
 
    /**
     * 進(jìn)入首頁
     *
     * @return 首頁頁面
     */
    @RequestMapping("/")
    public String index(){
        return "/index";
    }
 
    /**
     * 訪問登錄頁面時(shí),如果已經(jīng)登錄就轉(zhuǎn)發(fā)到首頁,未登錄就進(jìn)入登錄頁面
     *
     * @return 登錄頁面
     */
    @RequestMapping("/login")
    public String forward(){
        if(true){
            return "forward:/index";
        }
        return "login";
    }
}

3. 重定向

@Controller
public class IndexController {
 
    /**
     * 進(jìn)入首頁
     *
     * @return 首頁頁面
     */
    @RequestMapping("/")
    public String index(){
        return "/index";
    }
 
    /**
     * 訪問登錄頁面時(shí),如果已經(jīng)登錄就重定向到首頁,未登錄就進(jìn)入登錄頁面
     *
     * @return 登錄頁面
     */
    @RequestMapping("/login")
    public String redirect(){
        if(true){
            return "redirect:/index";
        }
        return "login";
    }
}

二、接收表單提交參數(shù)

1. 接收簡單包裝類型

模擬用戶登錄,接收用戶名、密碼、驗(yàn)證碼參數(shù)

    /**
     * 提交登陸信息
     *
     * @param username 用戶名
     * @param password 密碼
     * @param captcha  驗(yàn)證碼
     * @return 結(jié)果
     */
    @PostMapping("/login1")
    @ResponseBody
    public Map<String, Object> submitLogin1(String username, String password, String captcha) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "登錄成功");
        resultMap.put("username", username);
        resultMap.put("password", password);
        resultMap.put("captcha", captcha);
        return resultMap;
    }

2. 通過request獲取請求參數(shù)

    /**
     * 提交登陸信息
     *
     * @return 結(jié)果
     */
    @PostMapping("/login2")
    @ResponseBody
    public Map<String, Object> submitLogin2() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String captcha = request.getParameter("captcha");
 
 
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "登錄成功");
        resultMap.put("username", username);
        resultMap.put("password", password);
        resultMap.put("captcha", captcha);
        return resultMap;
    }

3. 接收對象類型

@Data
public class LoginUser {
 
    private String username;
 
    private String password;
 
    private String code;
}
    /**
     * 提交登陸信息
     *
     * @return 結(jié)果
     */
    @PostMapping("/login3")
    @ResponseBody
    public Map<String, Object> submitLogin3(LoginUser loginUser) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "登錄成功");
        resultMap.put("username", loginUser.getUsername());
        resultMap.put("password", loginUser.getPassword());
        resultMap.put("captcha", loginUser.getCaptcha());
        return resultMap;
    }

4.接收Map類型

    /**
     * 提交登陸信息
     *
     * @return 結(jié)果
     */
    @PostMapping("/login4")
    @ResponseBody
    public Map<String, Object> submitLogin4(@RequestParam Map<String, Object> loginUser) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "登錄成功");
        resultMap.put("username", loginUser.get("username"));
        resultMap.put("password", loginUser.get("password"));
        resultMap.put("captcha", loginUser.get("captcha"));
        return resultMap;
    }

5.接收數(shù)組類型

    /**
     * 修改角色權(quán)限
     *
     * @param userId 用戶id
     * @param roleIds 角色id
     * @return 結(jié)果
     */
    @PostMapping("/modifyRole1")
    @ResponseBody
    public Map<String, Object> modifyRole1(Integer userId, Integer[] roleIds) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "修改角色權(quán)限成功");
        resultMap.put("userId", userId);
        resultMap.put("roleIds", Arrays.toString(roleIds));
        return resultMap;
    }

6.接收List類型

    /**
     * 修改角色權(quán)限
     *
     * @param userId 用戶id
     * @param roleIds 角色id
     * @return 結(jié)果
     */
    @PostMapping("/modifyRole2")
    @ResponseBody
    public Map<String, Object> modifyRole2(Integer userId, @RequestParam("roleIds") List<Integer> roleIds) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "修改角色權(quán)限成功");
        resultMap.put("userId", userId);
        resultMap.put("roleIds", roleIds.toString());
        return resultMap;
    }

7.接收Set類型

    /**
     * 修改角色權(quán)限
     *
     * @param userId 用戶id
     * @param roleIds 角色id
     * @return 結(jié)果
     */
    @PostMapping("/modifyRole3")
    @ResponseBody
    public Map<String, Object> modifyRole3(Integer userId, @RequestParam("roleIds") Set<String> roleIds) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "修改角色權(quán)限成功");
        resultMap.put("userId", userId);
        resultMap.put("roleIds", roleIds.toString());
        return resultMap;
    }

8.接收帶List參數(shù)的實(shí)體類

@Data
public class ModifyRole {
 
    private Integer userId;
 
    private List<String> roleIds;
}
    /**
     * 修改角色權(quán)限
     *
     * @param modifyRole 數(shù)據(jù)
     * @return 結(jié)果
     */
    @PostMapping("/modifyRole4")
    @ResponseBody
    public Map<String, Object> modifyRole4(ModifyRole modifyRole) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "修改角色權(quán)限成功");
        resultMap.put("modifyRole", modifyRole.toString());
        return resultMap;
    }

三、接收J(rèn)SON參數(shù)

1. 接收帶List參數(shù)的實(shí)體類

@Data
public class ModifyRole {
 
    private Integer userId;
 
    private List<String> roleIds;
}
    /**
     * 修改角色權(quán)限
     *
     * @param modifyRole 數(shù)據(jù)
     * @return 結(jié)果
     */
    @PostMapping("/modifyRole5")
    @ResponseBody
    public Map<String, Object> modifyRole5(@RequestBody ModifyRole modifyRole) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "修改角色權(quán)限成功");
        resultMap.put("modifyRole", modifyRole.toString());
        return resultMap;
    }

2.接收List<Bean>類型

@Data
public class SysUser {
    
    private String username;
    
    private String password;
}
    /**
     * 批量新增用戶
     *
     * @param sysUserList 數(shù)據(jù)集合
     * @return 結(jié)果
     */
    @PostMapping("/batchInsert")
    @ResponseBody
    public Map<String, Object> batchInsert(@RequestBody List<SysUser> sysUserList) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "批量新增用戶成功");
        resultMap.put("modifyRoleList", sysUserList.toString());
        return resultMap;
    }

3.接收Set<Bean>類型

    /**
     * 批量新增用戶
     *
     * @param sysUserSet 數(shù)據(jù)集合
     * @return 結(jié)果
     */
    @PostMapping("/batchInsert2")
    @ResponseBody
    public Map<String, Object> batchInsert2(@RequestBody Set<SysUser> sysUserSet) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 200);
        resultMap.put("msg", "批量新增用戶成功");
        resultMap.put("modifyRoleSet", sysUserSet.toString());
        return resultMap;
    }

四、文件上傳、下載

1. 文件上傳

新建一個(gè)文件

    /**
     * 上傳文件
     *
     * @param multipartFile 上傳的文件
     * @return 結(jié)果
     */
    @PostMapping("/upload")
    @ResponseBody
    public void upload(MultipartFile multipartFile) throws IOException {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 獲取文件流
        InputStream inputStream = multipartFile.getInputStream();
        try (BufferedReader reader =  new BufferedReader(new InputStreamReader(inputStream, UTF_8.name()))){
            // 讀取文件數(shù)據(jù)
            String collect = reader.lines().collect(Collectors.joining());
            // 直接返回文件數(shù)據(jù)給前端
            attributes.getResponse().getWriter().write(collect);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

2. 文件下載

    /**
     * 文件下載
     */
    @GetMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        File file = ResourceUtils.getFile("classpath:application.yml");
 
        byte[] body;
        try (InputStream is = new FileInputStream(file)){
            body = new byte[is.available()];
            is.read(body);
        }
 
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + file.getName());
        return new ResponseEntity<>(body, headers, HttpStatus.OK);
    }

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

相關(guān)文章

  • SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置問題及解決方法

    SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置問題及解決方法

    這篇文章主要介紹了SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置,我們可以通過自定義配置數(shù)據(jù)庫配置類來解決這個(gè)問題,方式有很多,不同的業(yè)務(wù)采用的方式也不同,下面我簡單的介紹我們項(xiàng)目的使用的方法
    2022-06-06
  • Spring Boot配置AOP打印日志的全過程

    Spring Boot配置AOP打印日志的全過程

    這篇文章主要給大家介紹了關(guān)于Spring Boot配置AOP打印日志的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 詳解Java中AbstractMap抽象類

    詳解Java中AbstractMap抽象類

    本篇文章給大家詳細(xì)介紹了Java集合中的AbstractMap抽象類的相關(guān)用法以及知識(shí)點(diǎn)總結(jié),需要的朋友參考下。
    2018-03-03
  • 詳解Maven私服Nexus的安裝與使用

    詳解Maven私服Nexus的安裝與使用

    這篇文章主要介紹了詳解Maven私服Nexus的安裝與使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • 淺談Java內(nèi)部類——靜態(tài)內(nèi)部類

    淺談Java內(nèi)部類——靜態(tài)內(nèi)部類

    這篇文章主要介紹了Java靜態(tài)內(nèi)部類的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java內(nèi)部類的相關(guān)知識(shí),感興趣的朋友可以了解下
    2020-08-08
  • Redis工具類封裝RedisUtils的使用示例

    Redis工具類封裝RedisUtils的使用示例

    本文主要介紹了Redis工具類封裝RedisUtils的使用示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Flink作業(yè)Task運(yùn)行源碼解析

    Flink作業(yè)Task運(yùn)行源碼解析

    這篇文章主要為大家介紹了Flink作業(yè)Task運(yùn)行源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 歸并排序的實(shí)現(xiàn)代碼與思路

    歸并排序的實(shí)現(xiàn)代碼與思路

    歸并排序是建立在歸并操作上的一種有效的排序算法。該算法是采用分治法(Divide and Conquer)的一個(gè)非常典型的應(yīng)用。
    2013-03-03
  • springcloud + mybatis + seate集成示例

    springcloud + mybatis + seate集成示例

    本文主要介紹了springcloud + mybatis + seate集成示例,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • Spring Boot + Vue 前后端分離項(xiàng)目如何踢掉已登錄用戶

    Spring Boot + Vue 前后端分離項(xiàng)目如何踢掉已登錄用戶

    這篇文章主要介紹了Spring Boot + Vue 前后端分離項(xiàng)目如何踢掉已登錄用戶,需要的朋友可以參考下
    2020-05-05

最新評論

库伦旗| 昔阳县| 综艺| 广汉市| 遵义市| 青海省| 睢宁县| 太谷县| 襄樊市| 双牌县| 乌拉特后旗| 通化市| 于田县| 海伦市| 图片| 航空| 漯河市| 平谷区| 京山县| 莎车县| 铁岭市| 万安县| 碌曲县| 博罗县| 通城县| 和顺县| 遵化市| 山东| 巴青县| 香格里拉县| 蓬溪县| 洛阳市| 谢通门县| 五家渠市| 孝义市| 军事| 深水埗区| 武清区| 苏州市| 格尔木市| 新安县|