SpringMVC:数据的接收与返回

SpringMVC的定义:Spring Web MVC is the original web framework built on the Servlet API and included in the Spring Framework from the very beginning. 

在Spring中使用SpringMVC需要进行一系列的配置,如果用SpringBoot可以省去这些配置,本文主要总结SpringMVC中常用注解

必要注解

首先是@Controller,此注解加在类名上面,表示此类是一个有Spring管理的bean,同是也是springmvc中处理用户请求的组件之一。注解@RestController除了有@Controller的作用,还可以将此类下所有的成员方法的返回值的结果转换成json格式

注解@RequestMapping用来处理请求地址映射,其中的value值对应用户请求的地址,method值对应请求的方式(GET POST PUT DELETE. ...)

注解@GetMapping和@PostMapping分别对应处理get请求和post请求,算是@RequestMapping的增强简化版

package com.company.jelly.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class ProductController {

    @GetMapping("/index")
    public String add() {
        return "/product/form";
    }

    @PostMapping("/save")
    public ResponseEntity<?> save() {
        // do some
        return ResponseEntity.ok("success");
    }

与请求相关的注解

在发送请求时一般会传递数据,get请求和post请求接收参数的方式不同

GET请求:

  接收拼接在url后面的参数用@RequestParam,一般url格式是 http:www.hello.com/form?id=12,这其中的id就是参数

  接收镶嵌在url中的参数用@PathVariable,一般url格式是 http:www.hello.com/form/12,其中的12是参数

    @GetMapping("/form")
    public String add(@RequestParam Integer id) {
        return "/product/form";
    }

    @GetMapping("/form/{id}")
    public String keep(@PathVariable Integer id) {
        return "/product/form";
    }

 POST请求:

  post请求提交数据的方式有很多种(准确来说是几种格式,四种),可参考这篇博客(https://imququ.com/post/four-ways-to-post-data-in-http.html),最常见的是:浏览器原生form表单提交 和 json格式提交

  原生form表单提交,只需要在对应的方法参数中与接收到的数据(key-value格式)对应好key值就可以

  json格式提交,需要在方法参数中加上注解@RequestBody

    @PostMapping("/keep")
    public ResponseEntity<?> keep(Product product){
        // do some
        return ResponseEntity.ok("success");
    }

    @PostMapping("/save")
    public ResponseEntity<?> save(@RequestBody Product product) {
        // do some
        return ResponseEntity.ok("success");
    }

 如果数据格式层次较为复杂,上面两种方式都可以满足需求,可以查看我的这篇博客https://www.cnblogs.com/colin220/p/9532004.html。图片上传也另外写文章说明。一般来说,图片或者文件上传都是通过ajax异步上传到对应的文件服务器,然后返回图片或者文件的url,最后表单提交的不是图片本身而是图片的url

与响应相关的注解

SpringMVC默认返回视图(view),此时controller类中方法返回字符串,此字符串会被解析为对应路径下的视图文件,以前常用jsp,后面常用的模版引擎有FreeMarker(模版文件后缀为ftl)Thymeleaf(模版文件后缀为html)

后来流行前后端分离,后端只需要返回json数据,如果需要返回json数据,可以在controller中的方法上面加上@ResponseBody 或者 返回值为ResponseEntity

SpringMVC如果要 请求转发  重定向,只需要在controller的方法返回值的加上 forward(请求转发)  redirect(重定向)

    @GetMapping("/form")
    public String add(@RequestParam Integer id) {
        return "/product/form";
    }

    @PostMapping("/keep")
    @ResponseBody
    public String keep(Product product) {
        // do some
        return "success";
    }

    @PostMapping("/save")
    public ResponseEntity<?> save(@RequestBody Product product) {
        // do some
        return ResponseEntity.ok("success");
    }

 以上是SpringMVC基础用法,还有拦截器、过滤器等后面写文章补充

原文地址:https://www.cnblogs.com/colin220/p/9508815.html