我们在使用Spring MVC进行前端开发时,不可避免的需要Spring MVC中的controller来接收前端穿过来的参数,从而达到数据的传递。但Spring MVC接收前端传参方式并不是唯一的,本文仅使用get方法来进行演示,其他请求方法(POST,DELETE,PUT)接收参数的形式都是一样的。
下面是具体的Spring MVC接收前端传参方式:
1.接收数字类型的参数
通过get的url给后端传递参数,可以看到虽然在地址栏里 id=12345 中的12345是字符串的形式,但controller会自动把字符串转换成整型。如果把 id=12345 换成 id=name 就会报错。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 测试url: http://localhost:8080/int?id=12345
// 在浏览器中会显示 : 响应接收到的数字:12345
@GetMapping("/int")
@ResponseBody
public String ResInt(Integer id){
System.out.println("接收到的数字是:" + id);
return "响应接收到的数字:" + id;
}
}
2.接收字符串类型的参数
直接在地址栏中输入 http://localhost:8080/string?name=氵灬 就能看到结果。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 接收整数 测试url: http://localhost:8080/string?name=氵灬
// 浏览器中显示的数据应该是 : 响应接收到的数字:氵灬
@GetMapping("/string")
@ResponseBody
public String ResInt(String name){
System.out.println("接收到的字符串是:" + name);
return "响应接收到的数字:" + name;
}
}
3.通过 @RequestParam 来显示绑定接收的参数
如果前端在地址栏中输入的是 http://localhost:8080/string?user_name=氵灬 ,显然 user_name 这个字段不符合java中驼峰命名的规则。这这个时候就要使用 @RequestParam 注解来显示声明前端这个字段是用哪个参数来接收的。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 接收整数 测试url: http://localhost:8080/string?user_name=氵灬
// 浏览器中显示的数据应该是 : 响应接收到的数字:氵灬
@GetMapping("/string")
@ResponseBody
public String ResInt(@RequestParam("user_name") String userName){
System.out.println("接收到的字符串是:" + userName);
return "响应接收到的数字:" + userName;
}
}
@RequestParam 中可以配置两个参数一个是 value ,如果@RequestParam注解中只有一个参数,那value的值就是默认等于这个参数 @RequestParam("user_name") 的写法和 @RequestParam(value = "user_name") 效果是一样的。还是个参数就是 defaultValue 看字面意思就能知道是设置默认值的意思。如果前端传的这个参数为空则使用默认值提供的值。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 接收整数 测试url: http://localhost:8080/string?user_name=氵灬
// 浏览器中显示的数据应该是 : 响应接收到的数字:氵灬
// 接收整数 测试url: http://localhost:8080/string?user_name=
// 浏览器中显示的数据应该是 : 响应接收到的数字:默认值
@GetMapping("/string")
@ResponseBody
public String ResInt(@RequestParam(value = "user_name",defaultValue = "默认值") String userName){
System.out.println("接收到的字符串是:" + userName);
return "响应接收到的数字:" + userName;
}
}
4.通过对象来接收的参数
可以通过java对象来接收参数。通过对象来接收参数。先定义一个实体类,在再controller中使用
User.java
package com.cisbest.entity;
import lombok.Data;
@Data
public class User {
private String userName;
private Integer age;
private String sex;
}
CIsBestController.java
package com.cisbest.controller;
import com.cisbest.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 接收整数 测试url: http://localhost:8080/entity?name=氵灬&age=18&sex=男
// 浏览器中显示的数据应该是 : 响应接收到的数字:User(name=氵灬, age=18, sex=男)
@GetMapping("/entity")
@ResponseBody
public String ResInt(User user){
System.out.println("接收到的字符串是:" + user.toString());
return "响应接收到的数字:" + user.toString();
}
}
5.@RequestMapping
@RequestMapping 可以作用在类上也可以作用到方法上。
写在类上表示上一级请求路径。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/api")
public class CIsBestController {
// 访问路径 http://localhost:8080/api/req
@RequestMapping("/req")
@ResponseBody
public String resp() {
return "响应数据";
}
}
写在方法上:
-@RequestMapping("/req") 表示不区分请求类型。
-@RequestMapping(value = "/req",method = RequestMethod.POST) 表示这是一个POST请求。
在文章开头已经强调过,Spring MVC接收前端传参方式并不是唯一的,而且本文也只是使用了get方法进行演示的,想要全面了解Spring MVC接收前端传参方式的小伙伴可以在本站的Spring MVC视频教程中查看其它类型的请求方法。
QCode09-04 14:38
Code大师09-04 14:50
不写代码你养我啊08-23 11:14
不写代码你养我啊09-17 18:02
要学习了06-18 18:13