Springboot获取参数总结
1.直接获取
2.通过request.getParameter对象访问
3.JavaBean或者Dto直接访问
4.通过@PathVariable获取路径中的参数
5.使用@ModelAttribute注解获取 –form表单的提交
6.使用@RequestParam注解访问(可自定义默认值,和类型转换)
package com.abyiyi.controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
public class ParaMeterController {
/**
*
* 1 .直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交。
*
* 测试地址:http://localhost:8080/addUser1?username=lixiaoxi&password=111111
*
* @param username
* @param password
* @return
*/
@RequestMapping(value = "/addUser1",method = RequestMethod.GET)
public String addUser1(String username,String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "SUCCESS";
}
/**
* 2、通过HttpServletRequest接收
*
* post方式和get方式都可以。
*
* 测试地址:http://localhost:8080/addUser2?username=lixiaoxi&password=111111
*
* @param request
* @return
*/
@RequestMapping("/addUser2")
public String addUser2(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
/**
* 3、通过一个bean来接收
*
* post方式和get方式都可以。
* http://localhost:8080/addUser3?username=lixiaoxi&password=111111
*
* @param user
* @return
*/
@RequestMapping("/addUser3")
public String addUser3(UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}
/****
*
* 此次为测试,不规范的写法 *
*
*
*/
public class UserModel {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
* 4、通过@PathVariable获取路径中的参数
*
* 访问http://localhost:8080/addUser4/lixiaoxi/111111
*
* 自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=lixiaoxi、password=111111。
*
*
* @param username
* @param password
* @return
*/
@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
public String addUser4(@PathVariable String username,@PathVariable String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
/**
* 5、使用@ModelAttribute注解获取
*
* POST请求的FORM表单数据
*
* 适合form表单的提交
*
*
* @param user
* @return
*/
@RequestMapping(value="/addUser5",method=RequestMethod.POST)
public String addUser5(@ModelAttribute("user") UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}
/**
* 用注解@RequestParam绑定请求参数到方法入参
*
* post方式和get方式都可以。
*
* http://localhost:8080/addUser6?username=lixiaoxi&password=111111
*
* @param username
* @param password
* @return
*/
@RequestMapping(value="/addUser6",method=RequestMethod.POST)
public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
}
原文链接:https://blog.csdn.net/dong8633950/article/details/80818731