一、使用ServletAPI获取参数
通过 HttpServletRequest 当作形参,此时 HttpServletRequest 类型的参数表示封装了当前请求的请求报文的对象。
测试案例:
@RequestMapping("/testParam") public String testParam(HttpServletRequest request){ String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username:"+username+",password:"+password); return "success"; }
<form method="get" action="/SpringMVC/testParam"> username: <input type="text" name="username"/> password: <input type="password" name="password"/> <input type="submit" value="Submit" /> </form>
运行结果:
二、通过控制器方法的形参获取请求参数
在控制器方法的形参位置,设置和请求参数同名的形参,当浏览器发送请求,匹配到请求映射时,在 DispatcherServlet 中就会将请求参数赋值给相应的形参。
测试用例:
@RequestMapping("/testParam") public String testParam(String username,String password){ System.out.println("username:"+username+",password:"+password); return "success"; }
<form method="get" action="/SpringMVC/testParam"> username: <input type="text" name="username"/> password: <input type="password" name="password"/> <input type="submit" value="Submit" /> </form>
运行结果:
三、@RequestParam
@RequestParam是将请求参数和控制器方法的形参创建映射关系!
OK,我们可以来看一下 @RequestParam 的源码部分:
@Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestParam { //别名name,一般默认为空 @AliasFor("name") String value() default ""; //指定为形参赋值的请求参数的参数名 @AliasFor("value") String name() default ""; //设置是否必须传输此请求参数,默认值为true boolean required() default true; //用来定义默认值 String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n"; }
注:
若required设置为true时,则当前请求必须传输value所指定的请求参数,若没有传输该请求参数,且没有设置defaultValue属性,则页面报错 400:Required String parameter ‘xxx’ is not present; 若设置为false,则当前请求不是必须传输value所指定的请求参数,若没有传输,则注解所标识的形参的值为null。
@RequestMapping(value = "/testParam", method = RequestMethod.POST) public String testParam(@RequestParam(value = "user_name",required = false,defaultValue = "gg") String name, @RequestParam(value = "password",required = true,defaultValue = "hh") String pwd, String[] hobby) { System.out.println(name + pwd + Arrays.toString(hobby)); return "success"; }
<form th:action="@{/testParam}" method="post"> 用户名:<input type="name" name = "user_name"><br> 密码:<input type="password" name = "password"><br> 爱好:<input type="checkbox" name = "hobby" value="a">a <input type="checkbox" name = "hobby" value="b">b <input type="checkbox" name = "hobby" value="c">c<br> <input type="submit" value="测试"> </form>
运行结果:
四、@RequestHeader
@RequestHeader是将请求头信息和控制器方法的形参创建映射关系。
@RequestHeader注解一共有三个属性:value、required、defaultValue,用法同@RequestParam。
@RequestMapping(value = "/testParam2",method = RequestMethod.POST) //形参位置的request表示当前请求 public String testParam2(@RequestParam("username") String name, @RequestParam("password") String pwd, String[] hobby, @RequestHeader("Host") String host){ System.out.println(name+pwd+ Arrays.toString(hobby)); System.out.println("Host:"+host);//输出结果Host:localhost:8080 return "success"; }
五、@CookieValue
@CookieValue是将cookie数据和控制器方法的形参创建映射关系
@CookieValue注解一共有三个属性:value、required、defaultValue,用法同@RequestParam
@CookieValue("JSESSIONID") String JSESSIONID
@Controller
@RequestMapping("hello")
public class HelloController2 { @RequestMapping("show25") public String test25(Model model, @CookieValue("JSESSIONID")String jsessionid){ model.addAttribute("msg", "获取cookie,jsessionid:" + jsessionid); return "hello2"; } }
六、通过实体类的形参获取参数
首先需要创建一个实体类 User
package xiaobao.mvc.bean; public class User { private Integer id; private String username; private String password; private Integer age; private String sex; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } 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; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", email='" + email + '\'' + '}'; } }
然后前端:
<form th:action="@{/testpojo}" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> 性别:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br> 年龄:<input type="text" name="age"><br> 邮箱:<input type="text" name="email"><br> <input type="submit"> </form>
后端:
@RequestMapping("/testpojo") public String testPOJO(User user){ System.out.println(user); return "success"; }
运行结果: