SpringMVC中POJO对象和Servlet API作为入参

使用POJO对象绑定请求参数值

  • Spring MVC 会按请求参数名和POJO属性名进行自动匹配,自动为该对象填充属性值。支持级联属性。如:dept.deptId、dept.address.tel 等

  • 例子:

Controller层代码

1
2
3
4
5
@RequestMapping(value = "testPOJO",method = RequestMethod.POST)
public String testPOJO(User user){
System.out.println(user.toString());
return TEST;
}

jsp页面代码

1
2
3
4
5
6
7
8
9
10
11
<form action="testPOJO" method="post">
<input type="text" name="username" placeholder="用户名">
<br>
<input type="password" name="password" placeholder="密码">
<br>
<input type="text" name="address.province" placeholder="省份">
<br>
<input type="text" name="address.city" placeholder="城市">
<br>
<input type="submit" value="TestPOJO">
</form>

使用Servlet API作为入参

  • 例子:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    	/**
    * 可以使用 Serlvet 原生的 API 作为目标方法的参数 具体支持以下类型
    *
    * HttpServletRequest
    * HttpServletResponse
    * HttpSession
    * java.security.Principal
    * Locale InputStream
    * OutputStream
    * Reader
    * Writer
    * @throws IOException
    */
    @RequestMapping("/testServletAPI")
    public void testServletAPI(HttpServletRequest request,
    HttpServletResponse response, Writer out) throws IOException {
    System.out.println("testServletAPI, " + request + ", " + response);
    out.write("hello springmvc");
    // return SUCCESS;
    }
-------------本文结束感谢阅读-------------